Newbie Question: Lexically breaking up a line
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mike_Montana
Posted On:   Monday, January 23, 2006 04:40 PM

I'm a couple days into the docs and trying to apply an ANTLR grammar to validating a home grown Assembly-like language, but I'm misunderstaning some basic concept somewhere. Each line of this language can consist of 3 optional parts - a label, a mneumonic + parms, and/or a comment. A label is alphanuma ending with a ":" so I defined it as (I am omitting the other stuff in the .g file) LABEL: ('a'..'z')*':'; A menumonic is a 2 letter command with comma seperated values (upto 7) - defined as: STATEMENT : KEYWORD (PARMS)?; , KEYWORD : "ab"|"ac"|"cu"|"su"|"sw"|"ca"|"rt"; lastly, in    More>>

I'm a couple days into the docs and trying to apply an ANTLR grammar to validating a home grown Assembly-like language, but I'm misunderstaning some basic concept somewhere.


Each line of this language can consist of 3 optional parts - a label, a mneumonic + parms, and/or a comment.



A label is alphanuma ending with a ":" so I defined it as (I am omitting the other stuff in the .g file) LABEL: ('a'..'z')*':';


A menumonic is a 2 letter command with comma seperated values (upto 7) - defined as: STATEMENT : KEYWORD (PARMS)?; ,


KEYWORD : "ab"|"ac"|"cu"|"su"|"sw"|"ca"|"rt";


lastly, in this language a Comment is a single-line type starting with a ";" (defined as COMMENT : ';'(~('
'|'
'))* ('
'|'
'('
')?) {setType(Token.SKIP); newline(); System.out.println("COMMENT");}



My troubles are pretty basic - when running ANTLR, I get a list of complaints like 'nondeterminism between rules LABEL and STATEMENT' - and the execution against a sample file cant get beyond the 1st line of text. Why is LABEL and STATEMENT not distinct enough?



My parse class looks like: class CSVParser extends Parser;

options { k=2; } { int n=0; }

file {System.out.println("file called");}

: ( line (NEWLINE line)* (NEWLINE)? EOF)
{System.out.println("file matched");} ;



line {n++; System.out.println("line #"+n);}

:

c:COMMENT {System.out.println(" c " + c.getText());} |

l:LABEL {System.out.println(" l " + l.getText());} |

s:STATEMENT {System.out.println(" s " + s.getText());} ;



Can someone point me in the right direction? thanks, MikeMontana@hotmail.com

   <<Less
About | Sitemap | Contact