Hi Joe,
> I already posted a question along these lines, but perhaps the
> following example files will make the question more clear (and so,
> hopefully, easier to answer).
I am sorry for not replying to your previous post more quickly!
[...]
I can see two problems in your grammar:
1- there is a shift/reduce conflict on
state 4
expr -> expr . EQ expr (rule 3)
expr -> expr EQ expr . (rule 3)
EQ shift, and go to state 3
EQ [reduce using rule 3 (expr)]
$default reduce using rule 3 (expr)
___ To obtain the LALR grammar compiler detailed report, on the
"Grammar" menu, click on "LALR Compiler Verbose". Reports will be
appended in the "*wisent-log*" buffer. ___
It means that when the EQ lookahead token is encountered, the parser
will shift it on its stack, and continue with the next token. I guess
this is not what you want, which is probably to reduce the 'expr' on
left side first, then continue with EQ. This is a typical precedence
conflict that can be solved in that case with a "%left EQ" statement,
which indicates to reduce 'expr' when EQ is encountered:
[...]
> %type <keyword>
> %keyword EQ "="
%left EQ ;; Resolve S/R conflict in favor of reduce.
Anyway, I recommend you to read the Bison's manual which will give you
a good introduction to LALR grammar concepts, and conflicts and how to
solve them ;-)
2- There is a probably a bug in the following TAG expression:
[...]
> | expr EQ expr
> (TAG "expr eq" 'expr :value (concat "(equal "
> $1
> " "
> $3
> ")"))
Which will fail on `concat', because $1 and $2 aren't strings but
semantic tags! This one should work better:
(TAG "expr eq" 'expr :value (format "(equal %s %s)" $1 $3))
Finally, does it make sense that 'expr' can be empty?
Perhaps you meant something like the following?
------------------------------------------------
%start expression
%type <symbol>
%token <symbol> symbol "[A-Za-z][_A-Za-z0-9]*"
%type <string>
%token <string> STRING_LITERAL
%type <keyword>
%keyword EQ "="
%left EQ ;; Resolve S/R conflict in favor of reduce.
%%
;; For use with Semantic, must return valid semantic tags!
expression
: ;;Empty
| expr
(TAG "expr" 'expr :value $1)
;
expr
: symbol
| STRING_LITERAL
| expr EQ expr
(list 'EQ $1 $3)
;
%%
------------------------------------------------
I tried the above grammar with this small example:
a = "c"
"a" = b
"a" = "b"
a = b
a
"b"
And M-x bovinate displayed:
(("expr" expr
(:value
(EQ "a" "\"c\""))
nil #<overlay from 1 to 8 in test.simple>)
("expr" expr
(:value
(EQ "\"a\"" "b"))
nil #<overlay from 10 to 17 in test.simple>)
("expr" expr
(:value
(EQ "\"a\"" "\"b\""))
nil #<overlay from 19 to 28 in test.simple>)
("expr" expr
(:value
(EQ "a" "b"))
nil #<overlay from 30 to 35 in test.simple>)
("expr" expr
(:value "a")
nil #<overlay from 37 to 38 in test.simple>)
("expr" expr
(:value "\"b\"")
nil #<overlay from 40 to 43 in test.simple>))
Hope I helped. Good luck!
David
|