dcsimg
Question on the example "Preserving Whitespace During Translation"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ben_Yeung
Posted On:   Thursday, August 30, 2001 12:16 PM

I have read the article "http://www.antlr.org/fieldguide/whitespace/" and I have a hard time to understand the meaning of action involved ( "{#call = #(#[CALL,"CALL"], #call);} ). Can anyone spare some time to explain to me ? Thanks, Ben The complete parser rule is as follows: class InstrParser extends Parser; options { buildAST = true; k=2; } tokens { CALL // define imaginary token CALL } slist : ( stat )+ ; stat: LBRACE^ (stat)+ RBRACE | "if"^ expr "then" stat ("else" stat)?    More>>

I have read the article "http://www.antlr.org/fieldguide/whitespace/" and I have a hard time to understand the meaning of action involved

			
( "{#call = #(#[CALL,"CALL"], #call);} ).


Can anyone spare some time to explain to me ?


Thanks,
Ben

The complete parser rule is as follows:

			
class InstrParser extends Parser;
options {
buildAST = true;
k=2;
}

tokens {
CALL // define imaginary token CALL
}

slist
: ( stat )+
;

stat: LBRACE^ (stat)+ RBRACE
| "if"^ expr "then" stat ("else" stat)?
| ID ASSIGN^ expr SEMI
| call
;

expr
: mexpr (PLUS^ mexpr)*
;

mexpr
: atom (STAR^ atom)*
;

atom: INT
| ID
;

call: ID LPAREN (expr)? RPAREN SEMI
{#call = #(#[CALL,"CALL"], #call);}
^^^ This is line I don't understand.
;
   <<Less

Re: Question on the example "Preserving Whitespace During Translation"

Posted By:   Terence_Parr  
Posted On:   Saturday, September 1, 2001 11:46 AM

This action, {"{#call = #(#[CALL,"CALL"], #call);} ) says "set the tree created for this rule to be a tree with an 'imaginary' token called CALL at the root and what was automatically created underneath that." So, if the tree looks like the list ID LPAREN expr-subtree RPAREN SEMI then after that it will look like #( CALL ID LPAREN expr-subtree RPAREN SEMI) where I have just stuck a CALL node at the root.


This is done specifically to make walking the tree easier in the tree parser.

About | Sitemap | Contact