[antlr-interest] Please help a newbie!
Peter Ashford
kaffiene at xtra.co.nz
Tue Aug 3 20:32:12 PDT 2004
Richard Clark wrote:
>On Aug 3, 2004, at 15:06, Peter Ashford wrote:
>
>
>
>>Ah... I did wonder if I might have been using the AST gratutiously just
>>because I didn't understand the basics properly :o) If I can bug you
>>some more - how would I go about parsing the expressions without using
>>the tree?
>>
>>
>>
>
>Look at Terrence's "beginner's tutorial" -- it shows an expression
>parser w/o a tree walker.
><http://www.cs.usfca.edu/~parrt/course/652/lectures/antlr.html>
>
>...Richard
>
>
Thanks for that - I've gone through an refactored my grammar to only use
the one parser and it works fine (a bit simpler too for my simple
purposes :o))
And so now I 've reached my next problem :o)
I've added variable assignments to the grammar but they don't work. The
scanner reports unexpected char on the first character of an identifier
(e.g. "myval = 123" => "line 1:1: unexpected char 'm'")
I noticed the documentation talks about the testLiterals options, but
I've used that and it doesn 't seem to help.
- here's the parser rule:
assign
{ float f; }
: i:ID EQ f=f_expr { assignTo(i->getText(),
f); }
;
And here's the lexer rule:
protected
ALPHA : ('a'..'z' 'A'..'Z');
ID
options {
paraphrase = "an identifier";
testLiterals = true;
}
: ALPHA (ALPHA | DIGIT)*;
The whole grammar is printed below:
---------------------------------------------------------------------------
header {
extern void assignTo(std::string id, float value);
}
options {
language="Cpp";
genHashLines = true;
}
//=======================================================================
class MyParser extends Parser;
script returns [std::string value]
{std::string s; }
: (assign)* s=result { value = s; }
;
assign
{ float f; }
: i:ID EQ f=f_expr { assignTo(i->getText(), f); }
;
result returns [std::string value]
{float f;}
: RESULT EQ f=f_expr { char buf[127];
sprintf(buf,"%f", f);
value = std::string(buf); }
;
f_expr returns [float value=0]
{float f;}
: value=f_mexpr
( PLUS f=f_mexpr { value += f; }
| MINUS f=f_mexpr { value -= f; }
)*
;
f_mexpr returns [float value=0]
{ float f; }
: value=f_atom ( MULT f=f_atom { value *= f;}
| DIV f=f_atom { value /= f;}
)*
;
f_atom returns [float value=0]
{ float f; }
: n:NUMBER { value = (float)
atof(n->getText().c_str()); }
| LPAREN f=f_expr RPAREN { value = f; }
;
//=======================================================================
class MyLexer extends Lexer;
options {
k=2; // needed for newline junk
charVocabulary='\u0000'..'\u007F'; // allow ascii
testLiterals=false;
}
LPAREN : '(' ;
RPAREN : ')' ;
PLUS : '+' ;
MINUS : '-' ;
MULT : '*' ;
DIV : '/' ;
EQ : '=' ;
RESULT : "result";
protected
DIGIT : ('0'..'9');
protected
ALPHA : ('a'..'z' 'A'..'Z');
NUMBER
options {
paraphrase = "a number";
}
: (DIGIT)+ ( | ('.' (DIGIT)*));
ID
options {
paraphrase = "an identifier";
testLiterals = true;
}
: ALPHA (ALPHA | DIGIT)*;
STRING
options {
paraphrase = "a quoted string";
}
: '"'! (~'"') '"'!;
COMMENT: "//" (~'\n')* '\n' {_ttype =
ANTLR_USE_NAMESPACE(antlr)Token::SKIP;}
;
WS : ( ' '
| '\r' '\n'
| '\n'
| '\t'
)
{_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;}
;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040804/7f0ff038/attachment.html
More information about the antlr-interest
mailing list