dcsimg
TreeParser Question
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tobin_Jackson
Posted On:   Saturday, October 6, 2001 02:45 PM

I'm designing a language for a Compiler Design class and I'm working on the 'expression' part of the grammar. I have part of the Tree Parser for 'expression' working. I say 'part' because I could only figure out the grammar to add/sub or mult/div two values. My parser of course recognizes multiple elements. (ie. e: t(("*"^|"/"^)t)* ) Here's what i have for my TreeParser. Is there a way to use subrules or restructure the grammar to accept expressions like a+b+c or a*b*c? Any help would be appreciated.. thanks, tobin expression returns [int value] { int a; value = 0; } : a=addExpression { v   More>>


I'm designing a language for a Compiler Design class and I'm working on the 'expression' part of the grammar. I have part of the Tree Parser for 'expression' working. I say 'part' because I could only figure out the grammar to add/sub or mult/div two values.


My parser of course recognizes multiple elements. (ie. e: t(("*"^|"/"^)t)* )


Here's what i have for my TreeParser. Is there a way to use subrules or restructure the grammar to accept expressions like a+b+c or a*b*c? Any help would be appreciated..


thanks,


tobin



			
expression returns [int value] {
int a;
value = 0;
}
: a=addExpression { value = a;
;

addExpression returns [int value] {
int a,b,c,temp;
value = 0;
}
: #(PLUS a=multExpression b=multExpression) { value = a+b; }
| #(MINUS a=multExpression b=multExpression) { value = a-b; }
;

multExpression returns [int value] {
int a,b;
value = 0;
}
: #(STAR a=term b=term) { value = a*b; }
| #(DIV a=term b=term) { value = a*b; }
| a=term { value = a; }
;

term returns [int value] {
int a,b;
value = 0;
}
: i:INT_LIT { value = Integer.parseInt(i.getText()); }
| j:REAL_LIT { value = (int)Double.parseDouble(j.getText()); }
| m:ID { value = 555; }
| #(LPAREN a=addExpression) { value = a; }
;
   <<Less

Re: TreeParser Question

Posted By:   Terence_Parr  
Posted On:   Wednesday, October 10, 2001 04:34 PM

Hi. The parser should construct multi-level trees such as



(ADD a (ADD b c))




for a+b+c. All operators are binary so you have to add the result of an add to get the sequence.
About | Sitemap | Contact