[antlr-interest] Code Generation

Shomi Mahima shomi_mahima at yahoo.com
Wed Feb 11 04:33:42 PST 2004


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 comprises of  a (minimal)subset of the C++ language ( which is minimal enough to write a fairly descent program ) and certain Real 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 itself .
 
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 mean 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 grammar 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 different instructions differently for the rules.Please help me!!
 
2. Moreover consider the situation (Forgive me if the question is too trivial)
 
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 cs.utwente.nl> 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 away
> 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 templates
and probably some other things with operator overloading will require a lot
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 generate
> 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. Again
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 utwente.nl ----- +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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040211/81ec5cb4/attachment.html


More information about the antlr-interest mailing list