1
$\begingroup$

I have read that yacc generates bottom up parser for LALR(1) grammars. I have a grammar for Java 1 that can be used for generating three address code and is strictly LALR(1), but the translation scheme I am employing makes it L-attributed. Now I have read that L-attributed LR grammars cannot be parsed by using bottom up parsing. So, can yacc be used here or not?

$\endgroup$
0
$\begingroup$

A grammar's parser serves to recognize the language generated. Tools like YACC augment the parser to (essentially) trace a parse tree (some automatically build an AST, abstract syntax tree) for the parsed string according to the grammar. You can then walk this structure to generate code. But that is a separate task, that commonly uses different tools.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ I suppose yacc does bot actually generate and store the parse tree somewhere. It reduces strings of grammar symbols into nonterminals and as that reduction is performed, it performa the associated semantic action. So in a sense a virtual parse tree is built as parsing goes on, whic is essentially what you said in “trace a parse tree”. But I have read that translation of an L attributed LR grammar cannot accompany bottom up parsing. You must separately build a parse tree and then operate on that. $\endgroup$ – Shashank Kumar Apr 16 at 1:00
  • $\begingroup$ Yacc doesn't store any parse trees. You decide what rule to apply when a reduction occurs, whether that's generating a parse tree or whatever. $\endgroup$ – Pseudonym Apr 16 at 1:47
0
$\begingroup$

I'm not sure if this is what you're asking, but you can't write a single-pass Java compiler.

In C or C++, you have to declare (possibly forward-declare) a function before you use it. In Java, you don't. Since you need to know the declaration of a function/method in order to type check the call site, it follows that you can't type check a Java compilation unit until you have finished parsing it.

(Of course, you may be able to type check the three-address code, depending on the specifics of your representation.)

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.