Jad Sapida
Jad Sapida

Reputation: 57

Yacc/Lex: Rejecting symbols not defined

I was just wondering whether its possible to make YACC report a syntax error on all symbols not defined in a LEX file.

Eg.

Lex file

/*dummy.l*/
%{
#include "dummy.tab.h"

%%

int    return INT;
[a-z]+ return ID;
[0-9]+ return NUM;

%%

Yacc file

/*dummy.y*/
%token INT ID NUM

%% 
var : INT ID "=" NUM ";" 
%%

int main(void) {
    yyparse();
}

Say I have these 2 files, how do I make it so that my program will report a syntax error when a

$(dollar symbol) appears in the input.

Eg. It will still accept on input

int a = 234; $

NOTE: I want to reject all symbols that ARE NOT defined

Upvotes: 0

Views: 918

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126175

You need to add a lex rule to recognize all input text as some kind of token. Usually I use:

.|\n    return *yytext;

as the last pattern in the lex file. This insures that no input will trigger the lex default ECHO action. If you have another rule for \n (such as to ignore it), you don't need the \n here.

Another note -- using "=" (double quotes) in the yacc file will not do what you want -- you want to use '=' (single quotes) to match single literal characters.

Upvotes: 1

Related Questions