How to add precedence to LALR parser like in YACC?
Posted
by
greenoldman
on Programmers
See other posts from Programmers
or by greenoldman
Published on 2012-12-03T21:29:07Z
Indexed on
2012/12/03
23:22 UTC
Read the original article
Hit count: 278
parsing
Please note, I am asking about writing LALR parser, not writing rules for LALR parser.
What I need is...
...to mimic YACC precedence definitions. I don't know how it is implemented, and below I describe what I've done and read so far.
For now I have basic LALR parser written. Next step -- adding precedence, so 2+3*4
could be parsed as 2+(3*4)
.
I've read about precedence parsers, however I don't see how to fit such model into LALR. I don't understand two points:
how to compute when insert parenthesis generator
how to compute how many parenthesis the generator should create
I insert generators when the symbols is taken from input and put at the stack, right? So let's say I have something like this (|
denotes boundary between stack and input):
ID = 5 | + ...
, at this point I add open, so it givesID = < 5 | + ...
, then I read more inputID = < 5 + | 5 ...
and moreID = < 5 + 5 | ; ...
and moreID = < 5 + 5 ; | ...
At this point I should have several reduce moves in normal LALR, but the open parenthesis does not match so I continue reading more input. Which does not make sense.
So this was when problem.
And about count, let's say I have such data < 2 + < 3 * 4 >
. As human I can see that the last generator should create 2 parenthesis, but how to compute this? After all there could be two scenarios:
( 2 + ( 3 *4 ))
-- parenthesis is used to show the outcome of generatoror
(2 + (( 3 * 4 ) ^ 5)
because there was more input
Please note that in both cases before 3 was open generator, and after 4 there was close generator. However in both cases, after reading 4
I have to reduce, so I have to know what generator "creates".
© Programmers or respective owner