[antlr-interest] Help with Antlr
Sharon Li
hushlee83 at yahoo.com.sg
Tue Jan 28 16:35:30 PST 2003
Hi I seem to be running into problems with Antlr, so could some kind souls out there pls help.
My program needs to read a file that looks something like that :
/*--------Begining of file ----------------------------------*/
#define DURATION 100
#define MAX_INT_LEN 10
#define MAX_P_LEN 2
Lsystem: 0
/* number of steps in the simulation */
derivation length: 100
/* starting string or axiom */
/* I is an internode with parameter age,
* L is a leaf,
* B is a blossom,
* P is a petal with parameter age
*/
Axiom: I(1)[L]/(180)[L]I(1)I(1)I(1)B
I(age)-->I(age+1)
P(age)-->P(age+1)
R(ang)-->R(ang+3)
Decomposition
B --> R(1);(3)@o(1.5);(1)[P(1)]+(72)[P(1)]+(72)[P(1)]+(72)[P(1)]+(72)[P(1)]
Homomorphism
I(age)-->;(2)F(MAX_INT_LEN*func(1,age/DURATION))
L --> +(60);(4)F(1){.g(1)[-(90)g(1).][g(2).][+(90)g(1).]}
P(age) --> {.g(1)[-(90)g(1).][g(MAX_P_LEN*func(2,age/DURATION)).][+(90)g(1).]}
R(ang)--> +(ang)
/* Label for end of L-system */
endlsystem
/*----------- ENd of file -------------------------------------*/
"#define DURATION 100" declares a variable identifier DURATION and assigns a literal 100 to it. This value will be stored and substituted into:
P(age) --> {.g(1)[-(90)g(1).][g(MAX_P_LEN*func(2,age/DURATION)).][+(90)g(1).]}
The program should return me the substituted string.
My codes are as follow:
class LfileParser extends Parser;
options { k=2; }
{
LsystemFile file = new LsystemFile();
public LsystemFile getLsystemFile(){
return file;
}
}
declaration
: ((HEX)+ "define" (id:IDENT) (id2:IDENT))
{
String attr = id.getText();
String value = id2.getText();
System.out.println("#define "+attr+" "+value);
file.declare(attr, value);
}
;
derivationLength
: "derivation length" COLON (d : DIGIT)*
{ System.out.println("derivation length : "+d); } ;
lSystem
: "LSystem" COLON (d : DIGIT)*
;
expr: IDENT
| LPAREN expr RPAREN
| LBRACK expr RBRACK
| LCURLY expr RCURLY
;
axiom : "Axiom" "-->" (expr)
;
assign : expr "-->" (expr);
file : ( line (NEWLINE line)*(NEWLINE)? EOF)
{System.out.println("file matched");}
;
line : ((record)+ )
;
record : (declaration)* (lSystem) (derivationLength) (axiom) (assign)* "Decomposition" (assign)* "Homomorphism" (assign)* "endlsystem"
;
/********************************************* Lexer class *************************************************************************************/
class LfileLexer extends Lexer;
options{charVocabulary='\3'..'\377';}
protected LETTER : ( 'a'..'z' | 'A'..'Z' )+;
protected DIGIT : ('0'..'9')+ ;
protected SYMBOL : ('.'|','|';'|'_') ;
IDENT : (LETTER | DIGIT | SYMBOL);
LPAREN : '(' ;
RPAREN : ')' ;
LBRACK : '[' ;
RBRACK : ']' ;
LCURLY : '{' ;
RCURLY : '}' ;
HEX : '#' ;
COLON : ':' ;
COMMENT : "/*" (options {greedy=false;} :.)* "*/" ;
NEWLINE : ('\r''\n')=> '\r''\n' //DOS
| '\r' //MAC
| '\n' //UNIX
{ newline(); }
;
WS : (' '|'\t') { $setType(Token.SKIP); } ;
I'm actually pretty lost and help is very much appreciated.
P.S thanks to those who have helped me with it.
Yahoo! Travel
- Valentine surprise deals. Book now!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20030129/7d724551/attachment.html
More information about the antlr-interest
mailing list