dcsimg
who can help me???!!! parser problems...
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tiger_Liu
Posted On:   Friday, September 10, 2004 04:01 AM

Sorry that I have some difficulty to read the document the ANTLR offer, but I want to build my parser via it, Now I met some problem when I build it, here is the details: I write two files of lexer and parser, ************************************************** the lexer file is: ************************************************** options { mangleLiteralPrefix = "TK_"; } class ExprLexer extends Lexer; options { k=2; // needed for newline junk exportVocab=TZSM; charVocabulary='u0000'..'u007F'; // allow ascii } tokens { "if"; "else"; "then"; "end"; "and";   More>>

Sorry that I have some difficulty to read the document the ANTLR offer, but I want to build my parser via it,

Now I met some problem when I build it, here is the details:

I write two files of lexer and parser,

**************************************************
the lexer file is:
**************************************************
options {
mangleLiteralPrefix = "TK_";
}

class ExprLexer extends Lexer;

options {
k=2; // needed for newline junk
exportVocab=TZSM;
charVocabulary='u0000'..'u007F'; // allow ascii
}

tokens {
"if"; "else"; "then"; "end"; "and"; "or";
}

WS : (' '
| ' '
| '
' {newline();}
| '
')
{ _ttype = Token.SKIP; }
;


SL_COMMENT :
"//"
(~'
')* '
'
{ _ttype = Token.SKIP; newline(); }
;

ML_COMMENT
: "/*"
( { LA(2)!='/' }? '*'
| '
' { newline(); }
| ~('*'|'
')
)*
"*/"
{ $setType(Token.SKIP); }
;


LPAREN
options {
paraphrase="'('";
}
: '('
;

RPAREN
options {
paraphrase="')'";
}
: ')'
;

GREAT
: '>'
;

LESS
: ' <'
;

GRTEQ
: ">="
;

LESEQ
: " <="
;

NOTEQ
: "!="
;

UNION
: "||"
;

EQUAL
: '='
;

PLUS
: '+'
;

MINUS
: '-'
;

STAR
: '*'
;

DIV: '/'
;

SEMI
: ';'
;

COMMA
: ','
;

CHAR_LITERAL
: '\'' (ESC|~'\'') '\''
;

STRING_LITERAL
: '"' (ESC|~'"')* '"'
;

protected
ESC : '\\'
( 'n'
| 'r'
| 't'
| 'b'
| 'f'
| '"'
| '\''
| '\\'
| '0'..'3'
(
options {
warnWhenFollowAmbig = false;
}
: DIGIT
(
options {
warnWhenFollowAmbig = false;
}
: DIGIT
)?
)?
| '4'..'7'
(
options {
warnWhenFollowAmbig = false;
}
: DIGIT
)?
)
;

protected
DIGIT
: '0'..'9'
;

NUM : (DIGIT)+ ('.' (DIGIT)+)?
;

ID
options {
testLiterals = true;
paraphrase = "an identifier";
}
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;

**************************************************
the parser file is:
**************************************************

options {
mangleLiteralPrefix = "TK_";
}


class ExprParser extends Parser;
options {
k = 2; // two token lookahead
codeGenMakeSwitchThreshold = 2; // Some optimizations
codeGenBitsetTestThreshold = 3;
importVocab=TZSM; // use vocab generated by lexer
}

stat
: expr
| ifStat
;

ifStat
: TK_if ifPart TK_end TK_if
;

ifPart
: LPAREN expr RPAREN TK_then
stat
( TK_else stat
)?
;

expr
: (numExpr|strExpr)
(TK_and expr
|TK_or expr
)?
;

numExpr
: addExpr
(
EQUAL addExpr
|GREAT addExpr
|LESS addExpr
|GRTEQ addExpr
|LESEQ addExpr
|NOTEQ addExpr
)?
;

strExpr
: strAdd
(EQUAL strExpr
|NOTEQ strExpr
)?
;

strAdd
: strAtom
(UNION strAtom
)*
;

strAtom
: i:STRING_LITERAL
| LPAREN strExpr RPAREN
| id1:ID
;

addExpr
: mexpr
( PLUS mexpr
| MINUS mexpr
)*
;

mexpr
: atom
( STAR atom
| DIV atom
)*
;

atom
: i:NUM
| LPAREN addExpr RPAREN
| id:ID
;

**************************************************
when I compile it with antlr.Tool, the output sms is:
**************************************************

ANTLR Parser Generator Version 2.7.2 1989-2003 jGuru.com
parser.g:31:33: warning:nondeterminism between alts 1 and 2 of block upon
parser.g:31:33: k==1:LPAREN,ID
parser.g:31:33: k==2:"else","end","and","or",LPAREN,RPAREN,NOTEQ,EQUAL,ID

I know it's the nest problem, but how can I define to kill this problem?

Appreciate it much!

Tiger
2004/09/10

   <<Less
About | Sitemap | Contact