[antlr-interest] Re: Code Generation

lgcraymer lgc at mail1.jpl.nasa.gov
Wed Feb 11 13:37:31 PST 2004


Shomi--

All of these features have equivalents in ANTLR.  Check out 1.) rule return=
 values, 2.) the AST and Token getText() method, and 
3.) labels.  There are instances of use in the examples subdirectories: loo=
k at them, and read the documentation from www.antlr.org.  
Also, try to develop an understanding of ANTLR syntax trees--they really he=
lp to simplify translation.

--Loring


--- In antlr-interest at yahoogroups.com, Shomi Mahima <shomi_mahima at y...> wro=
te:
> Hello,
>  
> The grammar i have chosen is a sub-set of C++ language.I want to get the =
C++ language get compiled down to MSIL(Intermediate 
Language) of the virtual machine of the dotNET framework.My input grammar c=
omprises of  a (minimal)subset of the C++ language ( 
which is minimal enough to write a fairly descent program ) and certain Rea=
l time constructs ie,some constructs that aid in writing 
Realtime applications(as in the case of ADA) .
>  
> Since the constructs are less(I dont wanna take those complex  constructs=
 like operator overloading,inheritance etc) i thought of 
embedding the code generation phase within the semantic analysis phase itse=
lf .
>  
> In some tools an option has been provided
>  
> sum  := prod                    {$$ = $1.val  ;Emit($$.val) }
>             | sum+ prod         {$$ = $1 + $2} ;Emit(add)}
> prod   := term                   {$$=$1.val }
>            | prod * term 
> term   := int
>  
>  
> In this grammar (Yacc/Jay) it would be easy for generating the code.I mea=
n by the time the rule sum gets executed, the value for 
prod gets calculated and can be emitted.But since the way we mention the gr=
ammar is different in ANTLR, how can one do that?
>  
> sum : = (prod PLUS)?  prod 
>  
>  Since the rules are combined together into one how can one generate diff=
erent instructions differently for the rules.Please help me!!
>  
> 2. Moreover consider the situation (Forgive me if the question is too tri=
vial)
>  
> stmt   := intr : 
>                    INT   {System.Console.WriteLine(intr.getText())
>              PLUS
>              PLUS
>  
> Had i written the rules like this
>  
> intr   :  INT  
> stmt  :  intr PLUS PLUS
>  
> how can i emit the value of the INT .When i write the semantic action in =
stmt rule ,i get an error which says cannot return from 
another method intr()
> 
> Awaiting your reply,
> Shomi
>  
> 
> Ric Klaren <klaren at c...> wrote:
> Hi,
> 
> On Tue, Feb 10, 2004 at 03:17:03AM -0800, Shomi Mahima wrote:
> > The grammar that iam using is that of C++. What i meant was, instead of=

> > generating an AST and then walking the tree down to Emit the machine
> > code(MSIL of dotNET, using the Reflection.Emit API) , can i straight aw=
ay
> > emit the instructions within the actions in the grammar file?
> 
> My first impression would be that with C++ as input language there's no
> chance to support the full language in one pass. Instantiation of templat=
es
> and probably some other things with operator overloading will require a l=
ot
> of non-trivial processing.
> 
> C++ is very nasty to parse. Terence & David can probably give you more
> advice on parsing C++.
> 
> > Will it be an effective approach? If it were a LALR parser , I'll gener=
ate
> > the code when i make a reduce actions .In LL parser should i have to
> > separate a module for codegeneration ?
> 
> I doubt wether that will work in one pass with a LALR parser as well. Aga=
in
> it depends on how much of the C++ language you need to generate code for.=

> 
> (Or am I misunderstanding you and you want to use antlr in C++ mode for
> your project e.g. have antlr generate C++ files you compile and then
> generate for you specific problem (not C++ as input) code)
> 
> Cheers,
> 
> Ric
> --
> -----+++++*****************************************************+++++++++-=
------
> ---- Ric Klaren ----- j.klaren at u... ----- +31 53 4893722 ----
> -----+++++*****************************************************+++++++++-=
------
> Innovation makes enemies of all those who prospered under the old
> regime, and only lukewarm support is forthcoming from those who would
> prosper under the new. --- Niccolò Machiavelli
> 
> 
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 
> 
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Finance: Get your refund fast by filing online



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list