Erlang/OTP Forums

Author Message

<  Erlang  ~  Precedence handling in yecc of Parse Tools 1.4.5

Bill M.
Posted: Wed Mar 18, 2009 2:03 pm Reply with quote
User Joined: 06 Jun 2008 Posts: 24 Location: New York
Hello All:

Sorry if you get this twice, but to elicit a response, I am cross posting here from the mailing lists. I am using parse tools 1.4.5 on Erlang R12B4.

I was trying out leex and yecc, and tried to do the canonical infix expression parsing, but some strange software behaviors have me wondering if I made a cockpit error or if I've hit some bugs. The source code for the parser appears quoted at the bottom of this e-mail, as well as the build instructions I used.


1). Precedence handling in yecc did not have the desired results, using the grammar with precedence enabled gives the following errors.

Quote:

erl
Erlang (BEAM) emulator version 5.6.4 [source] [64-bit] [smp:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.4 (abort with ^G)
1> prefixer:parse("2 * 3\n").
In prefixer:parse(String=2 * 3
)
{prefixer:13}: "Tokens" -> [{num,1,{2,[]}},{'*',1},{num,1,{3,[]}},{eoln,2}]
ParseTree='$undefined'
'$undefined'
2> prefixer:parse("2 + 3\n").
In prefixer:parse(String=2 + 3
)
{prefixer:13}: "Tokens" -> [{num,1,{2,[]}},{'+',1},{num,1,{3,[]}},{eoln,2}]
** exception error: {yecc_bug,"1.3",{missing_state_in_action_table,10}}
in function prefixer_parser:yeccpars2/7
in call from prefixer_parser:yeccpars0/2
in call from prefixer:parse/1


However, if I don't use precedence in the grammar, the parser will accept (but incorrectly parse) expressions containing '+' or '-' (e.g. 3 + 4 * 5 parses similarly to (3 + 4) * 5 instead of the correct 3 + (4 * 5)). No shift/reduce or reduce/reduce errors are reported by yecc in either case.

2). Also the Header field didn't seem to work for me, yecc didn't like it. If I remove the leading comment and uncomment the remaining lines I get the following error building my parser:

Quote:


# Generate the parser
erl -I -pa ./ebin -pa ./eunit/ebin -noshell -eval 'yecc:file("src/prefixer_parser"), halt().'
src/prefixer_parser.yrl:7: syntax error before: Nonterminals



The parser reads:

Code:

%% In spite of the documentation yecc does not recognize the Header field
%% Header "%% @Doc parses simple expressions."
%% "%% @Private"
%% "%% @Copyright 2009"
%% "%% @Version 0.1"

Nonterminals prog expr term.

Terminals '+' '-' '*' '/' '(' ')' eoln num var.

Rootsymbol prog.

%% Uncommenting the following lines prevents parsing  inputs using the
%%  + or - operators!
%Left 100 '+'.
%Left 100 '-'.
%Left 200 '*'.
%Left 200 '/'.


prog -> expr eoln.
prog -> prog expr eoln.

expr -> term.
expr -> expr '+' expr.
expr -> expr '-' expr.
expr -> expr '*' expr.
expr -> expr '/' expr.

term -> '(' expr ')'.
term -> num.
term -> var.



Have I made coding errors or are these yecc bugs?

Thanks:

Bill M
View user's profile Send private message
rvirding
Posted: Thu Mar 19, 2009 10:37 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
A quick question: in the code listing it looks like the first comment is broken into 2 lines; is this so or is just that the line is too long?
View user's profile Send private message Visit poster's website MSN Messenger

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum