How to raise an error

View: New views
3 Messages — Rating Filter:   Alert me  

How to raise an error

by BradDaBug :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My parser basically works by calling various functions that build a big parse tree. Something like this:

identifier:
   LOCALIDENTIFIER { $$ = CreateIdentifierNode(yytext); }

The problem is that I need to be able to detect errors within those functions (for example, is the identifier name too long) and propagate them back to Bison. But I don't know how. I don't have access to the YYERROR macro in those functions, and I can't manually "goto yyerrorlab;" since I'm in a different function.

How can I do it?

Re: How to raise an error

by Luca-63 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

CreateIdentifierNode function can return a pointer to a struct (typically a pointer to a node).
You can set the error in a field inside the node; you have also to increase an error counter (increase
the same variable inside yyerror).

Then you can parse the remaining input, for example:

exp:
         INTEGER {$$ =AddIntNode($1);}
        |identifier {$$ = $1; }
        |exp '+' exp {$$ = AddNode('+',$1,$3); /*check the two children and returns a pNode */}

so AddIntNode and AddNode can recognize the error and perform the right action (so they can propagate the error up to the root of the syntax tree).
 

I suggest to read the follwing pdf:
http://epaperpress.com/lexandyacc/download/lexyacc.pdf
there is an example for a calculator using node.

Bye,
Luca


BradDaBug ha scritto:

> My parser basically works by calling various functions that build a big parse
> tree. Something like this:
>
> identifier:
>    LOCALIDENTIFIER { $$ = CreateIdentifierNode(yytext); }
>
> The problem is that I need to be able to detect errors within those
> functions (for example, is the identifier name too long) and propagate them
> back to Bison. But I don't know how. I don't have access to the YYERROR
> macro in those functions, and I can't manually "goto yyerrorlab;" since I'm
> in a different function.
>
> How can I do it?
>  



_______________________________________________
help-bison@... http://lists.gnu.org/mailman/listinfo/help-bison

Re: How to raise an error

by Mike Aubury :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Why not something like :



 identifier:
   LOCALIDENTIFIER {
              $$ = CreateIdentifierNode(yytext);
              if ($$== NULL) {
                      YYERROR;
              }
   }


Then just get your CreateIdentifierNode to return NULL if its invalid..
(You can play with setting an error buffer string to set the actual
error message etc..)

In my code - I tend to do something like :

   LOCALIDENTIFIER {
              char errbuff[256];
              $$ = CreateIdentifierNode(yytext,errbuff);
              if ($$== NULL) {
                      yyerror(errbuff);
                      YYERROR;
              }
   }

Where CreateIdentifierNode sets 'errbuff' when "CreateIdentifierNode"
returns NULL..



2009/10/9 BradDaBug <braddabug@...>:

>
> My parser basically works by calling various functions that build a big parse
> tree. Something like this:
>
> identifier:
>   LOCALIDENTIFIER { $$ = CreateIdentifierNode(yytext); }
>
> The problem is that I need to be able to detect errors within those
> functions (for example, is the identifier name too long) and propagate them
> back to Bison. But I don't know how. I don't have access to the YYERROR
> macro in those functions, and I can't manually "goto yyerrorlab;" since I'm
> in a different function.
>
> How can I do it?
> --
> View this message in context: http://www.nabble.com/How-to-raise-an-error-tp25824253p25824253.html
> Sent from the Gnu - Bison - Help mailing list archive at Nabble.com.
>
>
>
> _______________________________________________
> help-bison@... http://lists.gnu.org/mailman/listinfo/help-bison
>


_______________________________________________
help-bison@... http://lists.gnu.org/mailman/listinfo/help-bison