[antlr-interest] Help for a beginner
Eli Mesika
elim at tti-telecom.com
Sun Feb 24 09:29:05 PST 2002
Thanks
Your answers helped me very much.
Have a wonderful day ...
-----Original Message-----
From: Matthew Ford [mailto:Matthew.Ford at forward.com.au]
Sent: Sunday, February 24, 2002 7:19 PM
To: antlr-interest at yahoogroups.com
Subject: Re: [antlr-interest] Help for a beginner
>
> 1) statement is a rule, how do I get the text of each statement in
> the "(statement)+" expression in the proc rule.
I use
rule
{String str = null;}
:
.....
(str = statement
{ // process str here
}
)+
....
;
protected statement returns [String str]
{ str = "";}
: ( id:IDENT { str = id.getText();}
// etc
;
>
> 2) how do I get the value of the argument in the procArgs rule,I have
> tried (v:(ID|STRING_LITERAL|INT))* , that didn't work !
Try
rule
{ Token tok = null;}
:
....
( (id:ID{ tok = id;})
|( st:STRING_LITERAL {tok = st;})
| (i:INT{tok = i;})
{ // process tok here
switch tok.getType() {
case ID:
// process id here
System.out.println(tok.getText());
break;
case STRING_LITERAL:
// process st here
System.out.println(tok.getText());
break;
case INT:
// process i here
System.out.println(tok.getText());
break;
default:
throw new Exception(" bad token"+tok.toString());
}
}
)*
...
;
OR
(
(id:ID
{ // process id here
System.out.println(id.getText());
}
) | ( st:STRING_LITERAL
{ // process st here
System.out.println(st.getText());
}
) | (i:INT
{ // process i here
System.out.println(i.getText());
}
)
)*
matthew
----- Original Message -----
From: "elimesika" <elim at tti-telecom.com>
To: <antlr-interest at yahoogroups.com>
Sent: Monday, February 25, 2002 3:05 AM
Subject: [antlr-interest] Help for a beginner
> HI folks
> I am new to antlr and I have several questions:
>
> I have the following rules for my language parser :
>
> procArgs
> :
> LPAREN (ID|STRING_LITERAL|INT)* RPAREN
> ;
>
> proc
> :
> LPAREN PROC procArgs LPAREN (statement)+ RPAREN RPAREN
>
> ;
>
> questions:
>
> 1) statement is a rule, how do I get the text of each staement in
> the "(statement)+" expression in the proc rule.
>
> 2) how do I get the value of the argument in the procArgs rule,I have
> tried (v:(ID|STRING_LITERAL|INT))* , that didn't work !
>
> Thanks
> Eli
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list