How to throw error from lex/yacc (Dinesh Garg)
20 Jul 2004 19:46:49 -0400

          From comp.compilers

Related articles
How to throw error from lex/yacc (2004-07-20)
Re: How to throw error from lex/yacc (2004-07-28)
| List of all articles for this month |

From: (Dinesh Garg)
Newsgroups: comp.compilers
Date: 20 Jul 2004 19:46:49 -0400
Keywords: parse, errors, comment
Posted-Date: 20 Jul 2004 19:46:49 EDT

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 ':'
                            # 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 ?

Thanks a lot,
[This has come up many times before. It's easy enough to report
the exact place where the error was detected, but remembering
where you are in the lexer, but it's kind of tricky to tell
from the LALR parse tables what tokens would have been accepted.
Personally, I don't find the list of allowed tokens very useful
since they are misleading when you have errors like a missing
close brace. -John]

Post a followup to this message

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