[antlr-interest] Code Generation

Ric Klaren klaren at cs.utwente.nl
Tue Feb 10 02:48:35 PST 2004


Hi,

On Tue, Feb 10, 2004 at 09:26:21AM -0000, shomi_mahima wrote:
> Iam a new user of ANTLR.I have planned to design a compiler and am 
> using ANTLR for generating the parser and the scanner.Could some one 
> tell me whether embedding code in the grammar file  (that emits 
> Intermediate code of a virtual machine) is possible while writing 
> semantic actions in the grammar file.
> 1.  i wanna know whether embedding all the semantic checking  as well 
> as code generation in the grammar file is possible

If you mean just writing C++/C#/Java code as actions between the
lexer/grammar/treewalker rules then that's a yes. It will depend on your
grammar if you can do everything in one pass.

> 2.if possible how can one do that?(Please provide some links to some 
> example).I have studied many text books which suggests using LALR 
> parser for code generation

See the docs on antlr org. Download a distribution and have a look at the
examples directory. The files section on antlr.org might contain some
goodies as well. 

LALR has advantages at times LL works too, you only need to write things a
little different. Recursive descent parsers are a bit easier to debug due
to the readability of the generated code. Most free LALR parser have very
bad default error handling. ANTLR is a lot better with this respect
(although there are better ones) it generates pretty decent error messages
per default and if you put a bit time into it they can be greatly improved.

> 3. Since it is a LL parser will i get struck in the middle of the 
> development?

Depends on your grammar. But I would be surprised if you couldn't do it
with ANTLR.

> 4.It is mentioned that if some rules have common left prefixes
> 
>   say for eg) PR : "return"
>               PRV : "ret"
> here i have to make three look aheads.If supposei have two rules that 
> are similar in many characters, is it advisable to increase the 
> lookahead size?Is there any other way out?

Left factoring will fix that. Most compiler design books got sections on
recursive descent and left factoring. For a lexer rule you'd rewrite to
something like:

PR_OR_PRV: "ret" { $setType(PRV); } ( "urn" { $setType(PR); } )? ;

There's a lot of info on left factoring in the faq's on antlr at
www.jguru.com (although much of the faq is very outdated :( ) In general
it's rare if you have to use a lookahead bigger than 2-3 if you need to
have a bigger lookahead you don't have to worry too much either since ANTLR
will always use the smallest lookahead necessary for a decision. It only
makes code generation times bigger. Most special cases that occur with a
small lookahead can be fixed with semantic and syntactic predicates (at a
performance cost depending on the grammar)

ANTLR has a somewhat steep learning curve in the beginning (I guess mostly
due to the manual it has all the info but it's organization is less than
ideal) Especially ANTLR lexers take some getting used to if you come from
the lex/flex camp. But once you get your mind around it ANTLR works very
well.

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  There are 10 kinds of people - those that understand binary and
  those that don't. --- Unknown



 
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