Re: How to throw error from lex/yacc

kamalp@acm.org (Kamal R. Prasad)
28 Jul 2004 12:23:20 -0400

          From comp.compilers

Related articles
How to throw error from lex/yacc dinesh.garg@gmail.com (2004-07-20)
Re: How to throw error from lex/yacc kamalp@acm.org (2004-07-28)
| List of all articles for this month |

From: kamalp@acm.org (Kamal R. Prasad)
Newsgroups: comp.compilers
Date: 28 Jul 2004 12:23:20 -0400
Organization: http://groups.google.com
References: 04-07-065
Keywords: yacc, errors
Posted-Date: 28 Jul 2004 12:23:20 EDT

write your grammar as;-
Name : '/' TYPE1 ASSIGN value1 ':' TYPE_OBJECT {
                              # do some action
   }


            | '/' TYPE1 EQUALS value1 ':' TYPE_OBJECT {
                              fprintf(stderr, "expecting = but got ==\n");
   }


            ;


You would need to return ASSIGN from the lexer if there is a '=' on
the input stream not followed by another '=' and return EQUALS if the
input stream contains '=='.
Basically - it makes more sense to let yyerror(char* s) handle all
parsing errors.


regards
-kamal


dinesh.garg@gmail.com (Dinesh Garg) wrote in message news:04-07-065...
> Hi,
> I want to parse the grammar and throw error immediately i found out
> grammar is wrong. Grammer is something like this :
>
> Name : '/' TYPE1 '=' value1 ':' TYPE_OBJECT {
> # do some action
> }
>
> now suppose I want to throw some error I can change this to like this
>
> Name : '/' TYPE1 '=' value1 ':' TYPE_OBJECT {
> # do some action
> }
> | error { # do some thing}
>
> but problem with approach is if grammar does not match, I get the
> error message. but what i want is exactly where occured like
> /TYPE1==value:TYPE_OBJECT should give error expecting '=' instead of
> '=='
> if expression is like this
> /TYPE1=value:TYPE_OBJECT: should give error not expecting ':'
>
> How i tried is : put the state after every token like below
> Name : '/'{ chekcstate() } TYPE1 { chekcstate() } '=' value1 ':'
> TYPE_OBJECT {
> # do some action
> }
>
> but when these expression are reduced, sometimes due to ambiguities in
> rule as yacc does not differntiate depending upon action.
>
> so anyone have any idea how can i overcome this problem ?


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.