Link to home
Start Free TrialLog in
Avatar of unknown_
unknown_

asked on

flex + yacc/bison

Hello,

How can i integrate the yacc/bison grammar with my lexer ?
In specific, what is needed to be done in order to have the yyval not commented out ?

Thanks in advance for any help !
Avatar of unknown_
unknown_

ASKER


/* YACC */
 
 
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
main()
{
	yydebug=1;
	yyparse();
}
 
 
%token KEYWORDS STRING_LITERAL INT_LITERAL FLOAT_LITERAL VAR_LITERAL
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID FLOAT WHILE
 
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')'
;
 
 
keywords:	IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|FLOAT
			|WHILE
;
 
type_specifier: VOID
				| INT
				| FLOAT
;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement			
;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';'
 
;
 
return_statement:	RETURN ';'
					| RETURN expression ';'
;
 
expression: INTEGER 
			|
			VARIABLE
			|
			exp '+' exp
			|
			exp '-' exp
			|
			exp '*' exp
			|
			exp '/' exp
			| '(' expression ')'
;
 
operators:  '+'
			| '-'
			| '*'
			| '/'
 
;
 
puctuation: '('
			|')'
			|','
			|';'
			|'='
			|'%'
			|'|'
			|'||'
			|'&'
			|'&&'
			
;
 
 
%%
 
int main (int argc, char *argv[])
{
	
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}
 
 
 
 
 
/*   FLEX  */
 
 
%{
 
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_DO		11
#define T_RETURN	12
#define T_FLOAT		13
#define PLUS		14
#define MINUS		15
#define TIMES		16
#define SLASH		17
#define LPAREN		18
#define RPAREN		19	
#define SEMICOLON	20
#define COMMA		21
#define EQL		22
#define OR		23
#define OR2		24
#define AND		25
#define AND2		26
#define LITERAL	        27
#define IDENTIFIER	28
#define UNKNOWN  	29
 
 
		
	
		
%}
	
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
%%
							  
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return T_IF;       }
"else"               { return T_ELSE;     }
"do"                 { return T_DO;       }
"int"                { return T_INT;      }
"return"             { return T_RETURN;   }
"void"               { return T_VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return T_WHILE;   }
  
  
{LETTER}{LETTERDIGIT}* {
 return(IDENTIFIER);
 }
  
{VARIABLE}* { 
// printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
// strcpy(yyval.lexeme, yytext); 
 /*return T_VARIABLE*/
 return(VARIABLE);
 }
  
{COMMENT} {
 return(COMMENT);
 }
  
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
 return(FLOAT);
 }
  
{SIGN}?{DIGIT}+ {  
// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
 return(INTEGER);
 }
  
{STRINGCONSTANT} {
 return(LITERAL);
}
  
[ \t\n\r]            /* skip whitespace */
  
.                    { printf("Unknown character [%c]\n",yytext[0]);
		       return UNKNOWN;    }
 
%%
int main(void){
    int token;
    while(token = yylex()) {
    printf("lexed token: %d\n", token);
   }
}
 
int yywrap(void){return 1;}

Open in new window

First step is to get a working bison grammar that will compile, the same process we went through on your flex grammar.

You'll need to run bison on your grammar first, and if it is successful, it will produce a C header file, which will include the token declarations as well as declaration for YYSTYPE, which is what yylval is.

You'll need to #include that header file into the lex.l file, regenerate the lexer with flex, then compile again, but this time will require linking the whole program with both your lexer file and parser file, since yylval is defined over in your parser.

First, you need to try compiling your .y grammar and see what problems you have.

I get bison syntax errors immediately. You are missing both %{ and %} around your C section at the top, and missing token definitions. Your first rule uses token IDENTIFIER, yet IDENTIFIER is not defined in your %token section.

Remember, all of these %token definitions will need to match your lex code that uses those tokens. So where I had you #define all of the tokens in your lex grammar before, your new version will require that you remove those #defines and define all of the tokens in your bison file.
ok i commented out all the #defines from the mygrammar.l

the yacc returns these errors:
mygrammar.y:22: unrecognized '%' directive
mygrammar.y:23: unrecognized '%' directive
mygrammar.y:24: unrecognized '%' directive

Why is that happening ? :s
/* YACC */
 
%{
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
main()
{
	yydebug=1;
	yyparse();
}
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN YYSTYPE
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token T_IF T_ELSE T_DO T_INT T_RETURN T_VOID T_FLOAT T_WHILE
 
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|FLOAT
			|WHILE;
 
type_specifier: VOID
				| INT
				| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
			|
			VARIABLE
			|
			exp '+' exp
			|
			exp '-' exp
			|
			exp '*' exp
			|
			exp '/' exp
			| '(' expression ')';
 
operators:  '+'
			| '-'
			| '*'
			| '/';
 
puctuation: '('
			|')'
			|','
			|';'
			|'='
			|'%'
			|'|'
			|'||'
			|'&'
			|'&&';
 
 
%%
 
int main (int argc, char *argv[])
{
	
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}
 
 
 
 
 
/* FLEX */
 
%{
/*
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_DO		11
#define T_RETURN	12
#define T_FLOAT		13
#define PLUS		14
#define MINUS		15
#define TIMES		16
#define SLASH		17
#define LPAREN		18
#define RPAREN		19	
#define SEMICOLON	20
#define COMMA		21
#define EQL		22
#define OR		23
#define OR2		24
#define AND		25
#define AND2		26
#define LITERAL	        27
#define IDENTIFIER	28
#define UNKNOWN  	29
#define YYSTYPE  	30
*/
 
 
 
		
	
		
%}
	
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
%%
							  
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return T_IF;       }
"else"               { return T_ELSE;     }
"do"                 { return T_DO;       }
"int"                { return T_INT;      }
"return"             { return T_RETURN;   }
"void"               { return T_VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return T_WHILE;   }
  
  
{LETTER}{LETTERDIGIT}* {
 return(IDENTIFIER);
 }
  
{VARIABLE}* { 
// printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
// strcpy(yyval.lexeme, yytext); 
 /*return T_VARIABLE*/
 return(VARIABLE);
 }
  
{COMMENT} {
 return(COMMENT);
 }
  
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
 return(FLOAT);
 }
  
{SIGN}?{DIGIT}+ {  
// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
 return(INTEGER);
 }
  
{STRINGCONSTANT} {
 return(LITERAL);
}
  
[ \t\n\r]            /* skip whitespace */
  
.                    { printf("Unknown character [%c]\n",yytext[0]);
		       return UNKNOWN;    }
 
%%
int main(void){
    int token;
    while(token = yylex()) {
    printf("lexed token: %d\n", token);
   }
}
 
int yywrap(void){return 1;}

Open in new window

1) YYSTYPE is not a token, you should not redefine it. It is already defined by Yacc/Bison.
2) You are missing the start of your rules section, which is %%, put it on a line by itself before you define your first rule (primary_expression)

3)  You have more undefined tokens, you will see once you fix 1 and 2 and run bison on the file
Your expression rule is also using an undeclared token or rule called 'exp'

The intent of this rule is the heart of a recursive grammar. It is a recursive production.

An expression can be ( expression ), or expression + expression, etc.

This is recursion, just like if you called a function that called itself. Your naming is important. So what is 'exp' ? Nothing as far as Bison is concerned, it can't find a rule for it.

So what should you change exp '+' exp, and the other productions to? :)
 
why do i still get the error : unrecognized '%' directive ?

mygrammar.y:22: unrecognized '%' directive
mygrammar.y:23: unrecognized '%' directive
mygrammar.y:24: unrecognized '%' directive
mygrammar.y:29: unrecognized rule
mygrammar.y:30: unrecognized rule
mygrammar.y:31: unrecognized rule
mygrammar.y:32: unrecognized rule
mygrammar.y:33: unrecognized rule
mygrammar.y:34: unrecognized rule
mygrammar.y:38: unrecognized rule
mygrammar.y:39: unrecognized rule
mygrammar.y:40: unrecognized rule
mygrammar.y:41: unrecognized rule
mygrammar.y:42: unrecognized rule
mygrammar.y:43: unrecognized rule
mygrammar.y:44: unrecognized rule
mygrammar.y:47: unrecognized rule
mygrammar.y:48: unrecognized rule
mygrammar.y:51: unrecognized rule
mygrammar.y:52: unrecognized rule
mygrammar.y:53: unrecognized rule
mygrammar.y:57: unrecognized rule
mygrammar.y:60: unrecognized rule
mygrammar.y:63: unrecognized rule
mygrammar.y:65: unrecognized rule
mygrammar.y:67: unrecognized rule
mygrammar.y:69: unrecognized rule
mygrammar.y:71: unrecognized rule
mygrammar.y:73: unrecognized rule
mygrammar.y:76: unrecognized rule
mygrammar.y:77: unrecognized rule
mygrammar.y:78: unrecognized rule
mygrammar.y:81: unrecognized rule
mygrammar.y:82: unrecognized rule
mygrammar.y:83: unrecognized rule
mygrammar.y:84: unrecognized rule
mygrammar.y:85: unrecognized rule
mygrammar.y:86: unrecognized rule
mygrammar.y:87: unrecognized rule
mygrammar.y:88: unrecognized rule
mygrammar.y:89: unrecognized rule

%{
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
main()
{
	yydebug=1;
	yyparse();
}
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token T_IF T_ELSE T_DO T_INT T_RETURN T_VOID T_FLOAT T_WHILE
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |'|'
	    |'||'
	    |'&'
	    |'&&';
 
 
%%
 
int main (int argc, char *argv[])
{
	
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

What are you using, yacc, bison or byacc? I don't get the same errors that you do. Show me how you are compiling.
sorry that's the errors i get :
yacc mygrammar.y

mygrammar.y:40.26-31: symbol RETURN is used, but is not defined as a token and has no rules
mygrammar.y:37.26-29: symbol ELSE is used, but is not defined as a token and has no rules
mygrammar.y:41.26-29: symbol VOID is used, but is not defined as a token and has no rules
mygrammar.y:43.26-30: symbol WHILE is used, but is not defined as a token and has no rules
mygrammar.y:38.26-27: symbol DO is used, but is not defined as a token and has no rules
mygrammar.y:51.51-59: symbol statement is used, but is not defined as a token and has no rules
mygrammar.y:39.26-28: symbol INT is used, but is not defined as a token and has no rules
mygrammar.y:36.25-26: symbol IF is used, but is not defined as a token and has no rules
mygrammar.y:88.14-17: tokens '&' and '&&' both assigned number 38
mygrammar.y:86.14-17: tokens '|' and '||' both assigned number 124
So work one at a time. These are common errors, you must learn to fix the root cause.

1) What do you think "symbol RETURN is used, but is not defined as a token and has no rules" mean?

2) For the last 2 errors, do you think '&&' and '||' are legal C? Remember what single quotes mean in C.
ok now i get multiple warnings. was it expected ?

33 rules never reduced
mygrammar.y: warning: 7 useless nonterminals and 33 useless rules
mygrammar.y:36.1-8: warning: useless nonterminal: keywords
mygrammar.y:45.1-14: warning: useless nonterminal: type_specifier
mygrammar.y:49.1-11: warning: useless nonterminal: ifstatement
mygrammar.y:55.1-14: warning: useless nonterminal: whilestatement
mygrammar.y:58.1-16: warning: useless nonterminal: return_statement
mygrammar.y:74.1-9: warning: useless nonterminal: operators
mygrammar.y:79.1-10: warning: useless nonterminal: puctuation
mygrammar.y:36.25-26: warning: useless rule: keywords: IF
mygrammar.y:37.26-29: warning: useless rule: keywords: ELSE
mygrammar.y:38.26-27: warning: useless rule: keywords: DO
mygrammar.y:39.26-28: warning: useless rule: keywords: INT
mygrammar.y:40.26-31: warning: useless rule: keywords: RETURN
mygrammar.y:41.26-29: warning: useless rule: keywords: VOID
mygrammar.y:42.26-32: warning: useless rule: keywords: T_FLOAT
mygrammar.y:43.26-30: warning: useless rule: keywords: WHILE
mygrammar.y:45.17-20: warning: useless rule: type_specifier: VOID
mygrammar.y:46.19-21: warning: useless rule: type_specifier: INT
mygrammar.y:47.19-23: warning: useless rule: type_specifier: FLOAT
mygrammar.y:49.15-24: warning: useless rule: ifstatement: expression
mygrammar.y:50.29-51: warning: useless rule: ifstatement: VARIABLE '=' expression
mygrammar.y:51.29-59: warning: useless rule: ifstatement: IF '(' expression ')' statement
mygrammar.y:52.29-74: warning: useless rule: ifstatement: IF '(' expression ')' statement ELSE statement
mygrammar.y:55.17-50: warning: useless rule: whilestatement: WHILE '(' expression ')' statement
mygrammar.y:56.35-75: warning: useless rule: whilestatement: DO statement WHILE '(' expression ')' ';'
mygrammar.y:58.25-34: warning: useless rule: return_statement: RETURN ';'
mygrammar.y:59.27-47: warning: useless rule: return_statement: RETURN expression ';'
mygrammar.y:74.13-15: warning: useless rule: operators: '+'
mygrammar.y:75.15-17: warning: useless rule: operators: '-'
mygrammar.y:76.15-17: warning: useless rule: operators: '*'
mygrammar.y:77.15-17: warning: useless rule: operators: '/'
mygrammar.y:79.13-15: warning: useless rule: puctuation: '('
mygrammar.y:80.14-16: warning: useless rule: puctuation: ')'
mygrammar.y:81.14-16: warning: useless rule: puctuation: ','
mygrammar.y:82.14-16: warning: useless rule: puctuation: ';'
mygrammar.y:83.14-16: warning: useless rule: puctuation: '='
mygrammar.y:84.14-16: warning: useless rule: puctuation: '%'
mygrammar.y:85.14-16: warning: useless rule: puctuation: "|"
mygrammar.y:86.14-17: warning: useless rule: puctuation: "||"
mygrammar.y:87.14-16: warning: useless rule: puctuation: "&"
mygrammar.y:88.14-17: warning: useless rule: puctuation: "&&"
conflicts: 16 shift/reduce

%{
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
main()
{
	yydebug=1;
	yyparse();
}
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
int main (int argc, char *argv[])
{
	
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

The good news is you have compilation.

These other warnings are mostly because you have rules that don't reduce to anything because there is no reduction path to the start state.

Your grammar has to have a start state, which would usually be something like:

program, or compilation_unit, and all rules must in some way reduce back to the start state, or it is essentially an "unreachable" rule.

Lets start by adding an explicit start state, otherwise yacc will use the first rule. I advise you to start simple, so you can get your lexer + parser working together with a simple grammar.


%start expression


Add it right before your %% before you start your rules section.

ok
now i get :
40 rules never reduced
mygrammar.y: warning: 8 useless nonterminals and 40 useless rules
mygrammar.y:30.1-18: warning: useless nonterminal: primary_expression
mygrammar.y:39.1-8: warning: useless nonterminal: keywords
mygrammar.y:48.1-14: warning: useless nonterminal: type_specifier
mygrammar.y:52.1-11: warning: useless nonterminal: ifstatement
mygrammar.y:58.1-14: warning: useless nonterminal: whilestatement
mygrammar.y:61.1-16: warning: useless nonterminal: return_statement
mygrammar.y:77.1-9: warning: useless nonterminal: operators
mygrammar.y:82.1-10: warning: useless nonterminal: puctuation
mygrammar.y:30.21-30: warning: useless rule: primary_expression: IDENTIFIER
mygrammar.y:31.43-50: warning: useless rule: primary_expression: VARIABLE
mygrammar.y:32.43-49: warning: useless rule: primary_expression: COMMENT
mygrammar.y:33.43-47: warning: useless rule: primary_expression: FLOAT
mygrammar.y:34.43-49: warning: useless rule: primary_expression: INTEGER
mygrammar.y:35.43-49: warning: useless rule: primary_expression: LITERAL
mygrammar.y:36.43-60: warning: useless rule: primary_expression: '(' expression ')'
mygrammar.y:39.25-26: warning: useless rule: keywords: IF
mygrammar.y:40.26-29: warning: useless rule: keywords: ELSE
mygrammar.y:41.26-27: warning: useless rule: keywords: DO
mygrammar.y:42.26-28: warning: useless rule: keywords: INT
mygrammar.y:43.26-31: warning: useless rule: keywords: RETURN
mygrammar.y:44.26-29: warning: useless rule: keywords: VOID
mygrammar.y:45.26-32: warning: useless rule: keywords: T_FLOAT
mygrammar.y:46.26-30: warning: useless rule: keywords: WHILE
mygrammar.y:48.17-20: warning: useless rule: type_specifier: VOID
mygrammar.y:49.19-21: warning: useless rule: type_specifier: INT
mygrammar.y:50.19-23: warning: useless rule: type_specifier: FLOAT
mygrammar.y:52.15-24: warning: useless rule: ifstatement: expression
mygrammar.y:53.29-51: warning: useless rule: ifstatement: VARIABLE '=' expression
mygrammar.y:54.29-59: warning: useless rule: ifstatement: IF '(' expression ')' statement
mygrammar.y:55.29-74: warning: useless rule: ifstatement: IF '(' expression ')' statement ELSE statement
mygrammar.y:58.17-50: warning: useless rule: whilestatement: WHILE '(' expression ')' statement
mygrammar.y:59.35-75: warning: useless rule: whilestatement: DO statement WHILE '(' expression ')' ';'
mygrammar.y:61.25-34: warning: useless rule: return_statement: RETURN ';'
mygrammar.y:62.27-47: warning: useless rule: return_statement: RETURN expression ';'
mygrammar.y:77.13-15: warning: useless rule: operators: '+'
mygrammar.y:78.15-17: warning: useless rule: operators: '-'
mygrammar.y:79.15-17: warning: useless rule: operators: '*'
mygrammar.y:80.15-17: warning: useless rule: operators: '/'
mygrammar.y:82.13-15: warning: useless rule: puctuation: '('
mygrammar.y:83.14-16: warning: useless rule: puctuation: ')'
mygrammar.y:84.14-16: warning: useless rule: puctuation: ','
mygrammar.y:85.14-16: warning: useless rule: puctuation: ';'
mygrammar.y:86.14-16: warning: useless rule: puctuation: '='
mygrammar.y:87.14-16: warning: useless rule: puctuation: '%'
mygrammar.y:88.14-16: warning: useless rule: puctuation: "|"
mygrammar.y:89.14-17: warning: useless rule: puctuation: "||"
mygrammar.y:90.14-16: warning: useless rule: puctuation: "&"
mygrammar.y:91.14-17: warning: useless rule: puctuation: "&&"
conflicts: 16 shift/reduce


when you say to reduce back to the start expression do you mean setting up a rule:
start expression : primary_expression
                             | keywords
                             .....

??
%{
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
main()
{
	yydebug=1;
	yyparse();
}
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
%start expression
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
int main (int argc, char *argv[])
{
	
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

You did what I suggested, correctly. It might be better to use primary_expression as a start state.

Do that, then regenerate, then also try to compile the generated C code, see what happens. If you can get it to compile, clean, I'll help you add the yylval usage in, so you can integrate your lexer with the parser.
I compiled the y.tab.c but i cant understand why i get these errors :

mygrammar.y:7: error: syntax error before string constant
mygrammar.y:15: error: syntax error before } token
mygrammar.y: In function main:
mygrammar.y:27: warning: incompatible implicit declaration of built-in function exit
mygrammar.y:33: warning: incompatible implicit declaration of built-in function exit
mygrammar.y: At top level:
mygrammar.y:36: error: syntax error before } token

%{
#include <stdio.h>
 
extern FILE *yyin;
 
extern "C"
{
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
}
 
extern int yydebug;
 
int main (int argc, char *argv[])
{
	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		print (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		print (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}
}
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%

Open in new window

The warning about exit is because you need to include the header for it. I bet you know how to find out which standard include file is needed for exit()! :)

Regarding the character constants, extern "C" is a C++ construct, not a C one.

You are telling a C compiler "extern "C""

Either compile it with c++/g++ or remove the extern "C" {} wrapper altogether. Its only useful, anyway, if you are trying to export the functions as pure C from a C++ program, and since you are using the same compiler for all your code, its useless.

Now, after you fix that, you are going to see more errors regarding print() undefined. What is print? Should be fprintf() by your use of it, since you are passing stderr to it.

After you fix that, you are going to get linker errors regarding yyin and yylex and yyerror. Guess why? Because it is now time to compile both the lexer and parser together. Those symbols are from your lexer.

I recommend making yourself a build script to call flex, bison, then cc on both the .c files. Let me know when you reach this point.



Do you have man pages on OS X? I assume so since it is UNIX based and you have lex and yacc. Try "man 2 exit"
yeah sorry i forgot to include the headers

actually now i've got an error :

mygrammar.y:3: error: syntax error before * token

and two warnings:
mygrammar.y:3: warning: data definition has no type or storage class
mygrammar.y: In function main:
mygrammar.y:108: warning: assignment from incompatible pointer type

is that a linker error ?
%{
 
extern FILE *yyin;
 
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
 
extern int yydebug;
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
 
 
int main (int argc, char *argv[])
{
	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

syntax error is always a compile / parse error, never linker error.

What happened to your #include <stdio.h> ? You moved it down? Problem is, FILE * is at the top before any other includes, so the compiler doesn't know what FILE is.

Move your includes to the top.
so i assume these are linker errors:
Undefined symbols:
  "_yydebug", referenced from:
      _yydebug$non_lazy_ptr in ccYKpbwh.o
  "_yyerror", referenced from:
      _yyparse in ccYKpbwh.o
      _yyparse in ccYKpbwh.o
  "_yylex", referenced from:
      _yyparse in ccYKpbwh.o
  "_yyin", referenced from:
      _yyin$non_lazy_ptr in ccYKpbwh.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

what do you mean with "I recommend making yourself a build script to call flex, bison, then cc on both the .c files. Let me know when you reach this point."

Is it the step of including the .h file in the lexer ?



%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
 
extern FILE *yyin;
 
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
 
extern int yydebug;
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

I cannot believe your professor has you developing compilers before learning how to compile a multi-file C program. :|

Create a shell script (text file) named "go", as below.

Save it, then make it executable with:  chmod +x go

Then run it. Also, if flex & bison are available on your system, I recommend using those in place of lex & yacc.

#!/bin/sh
lex lexer.l
yacc parser.y
cc -o mycompiler y.tab.c lex.yy.c

Open in new window

i try to run the "go" but it returns:
33 rules never reduced
mygrammar.y: warning: 7 useless nonterminals and 33 useless rules
mygrammar.y:38.1-8: warning: useless nonterminal: keywords
mygrammar.y:47.1-14: warning: useless nonterminal: type_specifier
mygrammar.y:51.1-11: warning: useless nonterminal: ifstatement
mygrammar.y:57.1-14: warning: useless nonterminal: whilestatement
mygrammar.y:60.1-16: warning: useless nonterminal: return_statement
mygrammar.y:76.1-9: warning: useless nonterminal: operators
mygrammar.y:81.1-10: warning: useless nonterminal: puctuation
mygrammar.y:38.25-26: warning: useless rule: keywords: IF
mygrammar.y:39.26-29: warning: useless rule: keywords: ELSE
mygrammar.y:40.26-27: warning: useless rule: keywords: DO
mygrammar.y:41.26-28: warning: useless rule: keywords: INT
mygrammar.y:42.26-31: warning: useless rule: keywords: RETURN
mygrammar.y:43.26-29: warning: useless rule: keywords: VOID
mygrammar.y:44.26-32: warning: useless rule: keywords: T_FLOAT
mygrammar.y:45.26-30: warning: useless rule: keywords: WHILE
mygrammar.y:47.17-20: warning: useless rule: type_specifier: VOID
mygrammar.y:48.19-21: warning: useless rule: type_specifier: INT
mygrammar.y:49.19-23: warning: useless rule: type_specifier: FLOAT
mygrammar.y:51.15-24: warning: useless rule: ifstatement: expression
mygrammar.y:52.29-51: warning: useless rule: ifstatement: VARIABLE '=' expression
mygrammar.y:53.29-59: warning: useless rule: ifstatement: IF '(' expression ')' statement
mygrammar.y:54.29-74: warning: useless rule: ifstatement: IF '(' expression ')' statement ELSE statement
mygrammar.y:57.17-50: warning: useless rule: whilestatement: WHILE '(' expression ')' statement
mygrammar.y:58.35-75: warning: useless rule: whilestatement: DO statement WHILE '(' expression ')' ';'
mygrammar.y:60.25-34: warning: useless rule: return_statement: RETURN ';'
mygrammar.y:61.27-47: warning: useless rule: return_statement: RETURN expression ';'
mygrammar.y:76.13-15: warning: useless rule: operators: '+'
mygrammar.y:77.15-17: warning: useless rule: operators: '-'
mygrammar.y:78.15-17: warning: useless rule: operators: '*'
mygrammar.y:79.15-17: warning: useless rule: operators: '/'
mygrammar.y:81.13-15: warning: useless rule: puctuation: '('
mygrammar.y:82.14-16: warning: useless rule: puctuation: ')'
mygrammar.y:83.14-16: warning: useless rule: puctuation: ','
mygrammar.y:84.14-16: warning: useless rule: puctuation: ';'
mygrammar.y:85.14-16: warning: useless rule: puctuation: '='
mygrammar.y:86.14-16: warning: useless rule: puctuation: '%'
mygrammar.y:87.14-16: warning: useless rule: puctuation: "|"
mygrammar.y:88.14-17: warning: useless rule: puctuation: "||"
mygrammar.y:89.14-16: warning: useless rule: puctuation: "&"
mygrammar.y:90.14-17: warning: useless rule: puctuation: "&&"
conflicts: 16 shift/reduce
ld: duplicate symbol _main in /var/folders/TG/TGlZd13PHNiv66PPxhyIhk+++TI/-Tmp-//cco43DLJ.o and /var/folders/TG/TGlZd13PHNiv66PPxhyIhk+++TI/-Tmp-//cc9PEjIB.o
collect2: ld returned 1 exit status

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
 
extern FILE *yyin;
 
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
 
extern int yydebug;
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}
 
 
 
 
 
 
 
%{
 
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_DO		11
#define T_RETURN	12
#define T_FLOAT		13
#define PLUS		14
#define MINUS		15
#define TIMES		16
#define SLASH		17
#define LPAREN		18
#define RPAREN		19	
#define SEMICOLON	20
#define COMMA		21
#define EQL		22
#define OR		23
#define OR2		24
#define AND		25
#define AND2		26
#define LITERAL	        27
#define IDENTIFIER	28
#define UNKNOWN  	29
#define YYSTYPE  	30
 
		
%}
	
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
%%
							  
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return T_IF;       }
"else"               { return T_ELSE;     }
"do"                 { return T_DO;       }
"int"                { return T_INT;      }
"return"             { return T_RETURN;   }
"void"               { return T_VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return T_WHILE;   }
  
  
{LETTER}{LETTERDIGIT}* {
 return(IDENTIFIER);
 }
  
{VARIABLE}* { 
// printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
// strcpy(yyval.lexeme, yytext); 
 /*return T_VARIABLE*/
 return(VARIABLE);
 }
  
{COMMENT} {
 return(COMMENT);
 }
  
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
 return(FLOAT);
 }
  
{SIGN}?{DIGIT}+ {  
// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
 return(INTEGER);
 }
  
{STRINGCONSTANT} {
 return(LITERAL);
}
  
[ \t\n\r]            /* skip whitespace */
  
.                    { printf("Unknown character [%c]\n",yytext[0]);
		       return UNKNOWN;    }
 
%%
int main(void){
    int token;
    while(token = yylex()) {
    printf("lexed token: %d\n", token);
   }
}
 
int yywrap(void){return 1;}

Open in new window

Remember, you have a main in your lexer, for when the lexer was standalone.
You don't need it now, since your parser is calling the lexer as a "function", and your yacc program will have its own main.
Comment it the lexer main out by putting ifdef 0 around it, you might want it later for standalone testing again.

#if 0

#endif
I still get the same errors:

33 rules never reduced
mygrammar.y: warning: 7 useless nonterminals and 33 useless rules
mygrammar.y:38.1-8: warning: useless nonterminal: keywords
mygrammar.y:47.1-14: warning: useless nonterminal: type_specifier
mygrammar.y:51.1-11: warning: useless nonterminal: ifstatement
mygrammar.y:57.1-14: warning: useless nonterminal: whilestatement
mygrammar.y:60.1-16: warning: useless nonterminal: return_statement
mygrammar.y:76.1-9: warning: useless nonterminal: operators
mygrammar.y:81.1-10: warning: useless nonterminal: puctuation
mygrammar.y:38.25-26: warning: useless rule: keywords: IF
mygrammar.y:39.26-29: warning: useless rule: keywords: ELSE
mygrammar.y:40.26-27: warning: useless rule: keywords: DO
mygrammar.y:41.26-28: warning: useless rule: keywords: INT
mygrammar.y:42.26-31: warning: useless rule: keywords: RETURN
mygrammar.y:43.26-29: warning: useless rule: keywords: VOID
mygrammar.y:44.26-32: warning: useless rule: keywords: T_FLOAT
mygrammar.y:45.26-30: warning: useless rule: keywords: WHILE
mygrammar.y:47.17-20: warning: useless rule: type_specifier: VOID
mygrammar.y:48.19-21: warning: useless rule: type_specifier: INT
mygrammar.y:49.19-23: warning: useless rule: type_specifier: FLOAT
mygrammar.y:51.15-24: warning: useless rule: ifstatement: expression
mygrammar.y:52.29-51: warning: useless rule: ifstatement: VARIABLE '=' expression
mygrammar.y:53.29-59: warning: useless rule: ifstatement: IF '(' expression ')' statement
mygrammar.y:54.29-74: warning: useless rule: ifstatement: IF '(' expression ')' statement ELSE statement
mygrammar.y:57.17-50: warning: useless rule: whilestatement: WHILE '(' expression ')' statement
mygrammar.y:58.35-75: warning: useless rule: whilestatement: DO statement WHILE '(' expression ')' ';'
mygrammar.y:60.25-34: warning: useless rule: return_statement: RETURN ';'
mygrammar.y:61.27-47: warning: useless rule: return_statement: RETURN expression ';'
mygrammar.y:76.13-15: warning: useless rule: operators: '+'
mygrammar.y:77.15-17: warning: useless rule: operators: '-'
mygrammar.y:78.15-17: warning: useless rule: operators: '*'
mygrammar.y:79.15-17: warning: useless rule: operators: '/'
mygrammar.y:81.13-15: warning: useless rule: puctuation: '('
mygrammar.y:82.14-16: warning: useless rule: puctuation: ')'
mygrammar.y:83.14-16: warning: useless rule: puctuation: ','
mygrammar.y:84.14-16: warning: useless rule: puctuation: ';'
mygrammar.y:85.14-16: warning: useless rule: puctuation: '='
mygrammar.y:86.14-16: warning: useless rule: puctuation: '%'
mygrammar.y:87.14-16: warning: useless rule: puctuation: "|"
mygrammar.y:88.14-17: warning: useless rule: puctuation: "||"
mygrammar.y:89.14-16: warning: useless rule: puctuation: "&"
mygrammar.y:90.14-17: warning: useless rule: puctuation: "&&"
conflicts: 16 shift/reduce
ld: duplicate symbol _yywrap in /var/folders/TG/TGlZd13PHNiv66PPxhyIhk+++TI/-Tmp-//ccTpZH1u.o and /var/folders/TG/TGlZd13PHNiv66PPxhyIhk+++TI/-Tmp-//cc2DHZtu.o
collect2: ld returned 1 exit status

%{
 
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_DO		11
#define T_RETURN	12
#define T_FLOAT		13
#define PLUS		14
#define MINUS		15
#define TIMES		16
#define SLASH		17
#define LPAREN		18
#define RPAREN		19	
#define SEMICOLON	20
#define COMMA		21
#define EQL		22
#define OR		23
#define OR2		24
#define AND		25
#define AND2		26
#define LITERAL	        27
#define IDENTIFIER	28
#define UNKNOWN  	29
#define YYSTYPE  	30
 
		
%}
	
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
%%
							  
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return T_IF;       }
"else"               { return T_ELSE;     }
"do"                 { return T_DO;       }
"int"                { return T_INT;      }
"return"             { return T_RETURN;   }
"void"               { return T_VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return T_WHILE;   }
  
  
{LETTER}{LETTERDIGIT}* {
 return(IDENTIFIER);
 }
  
{VARIABLE}* { 
// printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
// strcpy(yyval.lexeme, yytext); 
 /*return T_VARIABLE*/
 return(VARIABLE);
 }
  
{COMMENT} {
 return(COMMENT);
 }
  
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
 return(FLOAT);
 }
  
{SIGN}?{DIGIT}+ {  
// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
 return(INTEGER);
 }
  
{STRINGCONSTANT} {
 return(LITERAL);
}
  
[ \t\n\r]            /* skip whitespace */
  
.                    { printf("Unknown character [%c]\n",yytext[0]);
		       return UNKNOWN;    }
 
%%
#if 0
int main(void){
    int token;
    while(token = yylex()) {
    printf("lexed token: %d\n", token);
   }
}
#endif
 
int yywrap(void){return 1;}
 
 
 
 
 
 
 
 
 
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
 
extern FILE *yyin;
 
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
 
extern int yydebug;
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}

Open in new window

That is not the exact same error, its a different symbol. You should know by now what is causing a duplicate symbol. This is basic C.

You are combining 2 C files. How did you fix the last duplicate symbol? Take the same approach with this one.

You only need one yywrap() for a lex/yacc parser.

Ok i still get the warnings but no more errors.
also "mycompiler" executable is created.
I try to run it but i get "Next token is token $undefined ()"

                                                                                                               (null)
Cleanup: discarding lookahead token $undefined ()
Stack now 0
Failed to open input file '(null)'




warnings:

33 rules never reduced
mygrammar.y: warning: 7 useless nonterminals and 33 useless rules
mygrammar.y:47.1-8: warning: useless nonterminal: keywords
mygrammar.y:56.1-14: warning: useless nonterminal: type_specifier
mygrammar.y:60.1-11: warning: useless nonterminal: ifstatement
mygrammar.y:66.1-14: warning: useless nonterminal: whilestatement
mygrammar.y:69.1-16: warning: useless nonterminal: return_statement
mygrammar.y:85.1-9: warning: useless nonterminal: operators
mygrammar.y:90.1-10: warning: useless nonterminal: puctuation
mygrammar.y:47.25-26: warning: useless rule: keywords: IF
mygrammar.y:48.26-29: warning: useless rule: keywords: ELSE
mygrammar.y:49.26-27: warning: useless rule: keywords: DO
mygrammar.y:50.26-28: warning: useless rule: keywords: INT
mygrammar.y:51.26-31: warning: useless rule: keywords: RETURN
mygrammar.y:52.26-29: warning: useless rule: keywords: VOID
mygrammar.y:53.26-32: warning: useless rule: keywords: T_FLOAT
mygrammar.y:54.26-30: warning: useless rule: keywords: WHILE
mygrammar.y:56.17-20: warning: useless rule: type_specifier: VOID
mygrammar.y:57.19-21: warning: useless rule: type_specifier: INT
mygrammar.y:58.19-23: warning: useless rule: type_specifier: FLOAT
mygrammar.y:60.15-24: warning: useless rule: ifstatement: expression
mygrammar.y:61.29-51: warning: useless rule: ifstatement: VARIABLE '=' expression
mygrammar.y:62.29-59: warning: useless rule: ifstatement: IF '(' expression ')' statement
mygrammar.y:63.29-74: warning: useless rule: ifstatement: IF '(' expression ')' statement ELSE statement
mygrammar.y:66.17-50: warning: useless rule: whilestatement: WHILE '(' expression ')' statement
mygrammar.y:67.35-75: warning: useless rule: whilestatement: DO statement WHILE '(' expression ')' ';'
mygrammar.y:69.25-34: warning: useless rule: return_statement: RETURN ';'
mygrammar.y:70.27-47: warning: useless rule: return_statement: RETURN expression ';'
mygrammar.y:85.13-15: warning: useless rule: operators: '+'
mygrammar.y:86.15-17: warning: useless rule: operators: '-'
mygrammar.y:87.15-17: warning: useless rule: operators: '*'
mygrammar.y:88.15-17: warning: useless rule: operators: '/'
mygrammar.y:90.13-15: warning: useless rule: puctuation: '('
mygrammar.y:91.14-16: warning: useless rule: puctuation: ')'
mygrammar.y:92.14-16: warning: useless rule: puctuation: ','
mygrammar.y:93.14-16: warning: useless rule: puctuation: ';'
mygrammar.y:94.14-16: warning: useless rule: puctuation: '='
mygrammar.y:95.14-16: warning: useless rule: puctuation: '%'
mygrammar.y:96.14-16: warning: useless rule: puctuation: "|"
mygrammar.y:97.14-17: warning: useless rule: puctuation: "||"
mygrammar.y:98.14-16: warning: useless rule: puctuation: "&"
mygrammar.y:99.14-17: warning: useless rule: puctuation: "&&"
conflicts: 16 shift/reduce

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define YYDEBUG 1
 
extern void yyerror(const char *);
extern yylex(), yyparse();
 
extern FILE *yyin;
 
	int yyparse(void);
	int yylex(void);  
	int yywrap()
	{
		return 1;
	}
	
 
extern int yydebug;
 
void yyerror(const char *s)
{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
}
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
 	yydebug=1;
	yyparse();
	
	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
    }
	
	if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
    }
}
 
 
 
 
 
 
 
 
 
%{
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_DO		11
#define T_RETURN	12
#define T_FLOAT		13
#define PLUS		14
#define MINUS		15
#define TIMES		16
#define SLASH		17
#define LPAREN		18
#define RPAREN		19	
#define SEMICOLON	20
#define COMMA		21
#define EQL		22
#define OR		23
#define OR2		24
#define AND		25
#define AND2		26
#define LITERAL	        27
#define IDENTIFIER	28
#define UNKNOWN  	29
#define YYSTYPE  	30
 
 
		
%}
	
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
%%
							  
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return T_IF;       }
"else"               { return T_ELSE;     }
"do"                 { return T_DO;       }
"int"                { return T_INT;      }
"return"             { return T_RETURN;   }
"void"               { return T_VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return T_WHILE;   }
  
  
{LETTER}{LETTERDIGIT}* {
 return(IDENTIFIER);
 }
  
{VARIABLE}* { 
 //printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
 //strcpy(yyval.lexeme, yytext); 
 /*return T_VARIABLE*/
 return(VARIABLE);
 }
  
{COMMENT} {
 return(COMMENT);
 }
  
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
 return(FLOAT);
 }
  
{SIGN}?{DIGIT}+ {  
// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
 return(INTEGER);
 }
  
{STRINGCONSTANT} {
 return(LITERAL);
}
  
[ \t\n\r]            /* skip whitespace */
  
.                    { printf("Unknown character [%c]\n",yytext[0]);
		       return UNKNOWN;    }
 
%%
#if 0
int main(void){
    int token;
    while(token = yylex()) {
    printf("lexed token: %d\n", token);
   }
}
#endif

Open in new window

1) You forgot what I told you about declaring tokens. Why did you un-comment the #define's for all of the tokens in your lexer. Remember, you don't need them there now that you have your yacc grammar.

Since its confusing you, just remove them completely from your lexer now. They all need to be defined with %token in your yacc grammar.

Also, just remove the main() from your lexer too, that may confuse you going forward.


2) DONT define YYSTYPE as a token, it is not a token.

3) Lastly, look at your main. How are you going to get the program parsed when you are calling yyparse() at the top, before you have even opened the file. yyin must be set to start parsing, or it will default to stdin. You must initialize yyin by opening the file BEFORE you parse. yacc (yyparse) calls lex (yylex) which gets its input from the yyin FILE handle.

but the point is when i uncomment the #define's i get:

mygrammar.y: In function main:
mygrammar.y:119: error: syntax error before else
mygrammar.l: In function yylex:
mygrammar.l:49: error: PLUS undeclared (first use in this function)
mygrammar.l:49: error: (Each undeclared identifier is reported only once
mygrammar.l:49: error: for each function it appears in.)
mygrammar.l:50: error: MINUS undeclared (first use in this function)
mygrammar.l:51: error: TIMES undeclared (first use in this function)
mygrammar.l:52: error: SLASH undeclared (first use in this function)
mygrammar.l:53: error: LPAREN undeclared (first use in this function)
mygrammar.l:54: error: RPAREN undeclared (first use in this function)
mygrammar.l:55: error: SEMICOLON undeclared (first use in this function)
mygrammar.l:56: error: COMMA undeclared (first use in this function)
mygrammar.l:57: error: EQL undeclared (first use in this function)
mygrammar.l:58: error: OR undeclared (first use in this function)
mygrammar.l:59: error: OR2 undeclared (first use in this function)
mygrammar.l:60: error: AND undeclared (first use in this function)
mygrammar.l:61: error: AND2 undeclared (first use in this function)
mygrammar.l:62: error: T_IF undeclared (first use in this function)
mygrammar.l:63: error: T_ELSE undeclared (first use in this function)
mygrammar.l:64: error: T_DO undeclared (first use in this function)
mygrammar.l:65: error: T_INT undeclared (first use in this function)
mygrammar.l:66: error: T_RETURN undeclared (first use in this function)
mygrammar.l:67: error: T_VOID undeclared (first use in this function)
mygrammar.l:68: error: T_FLOAT undeclared (first use in this function)
mygrammar.l:69: error: T_WHILE undeclared (first use in this function)
mygrammar.l:73: error: IDENTIFIER undeclared (first use in this function)
mygrammar.l:80: error: VARIABLE undeclared (first use in this function)
mygrammar.l:84: error: COMMENT undeclared (first use in this function)
mygrammar.l:89: error: FLOAT undeclared (first use in this function)
mygrammar.l:94: error: INTEGER undeclared (first use in this function)
mygrammar.l:98: error: LITERAL undeclared (first use in this function)
mygrammar.l:104: error: UNKNOWN undeclared (first use in this function)




Also about the yyin, is it something like that:  ?

      if ( argc > 0 )
      {
            yyin = fopen( argv[0], "r" );
      else
            yyin = stdin;
        yylex();
    }
      
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
| VARIABLE
| COMMENT
| FLOAT
| INTEGER
| LITERAL
| '(' expression ')';
 
 
keywords:	        IF
|ELSE
|DO
|INT
|RETURN 
|VOID 
|T_FLOAT
|WHILE;
 
type_specifier: VOID
| INT
| FLOAT;
 
ifstatement:  expression                      
| VARIABLE '=' expression
| IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
| RETURN expression ';';
 
expression: INTEGER 
|
VARIABLE
|
expression '+' expression
|
expression '-' expression
|
expression '*' expression
|
expression '/' expression
| '(' expression ')';
 
operators:  '+'
| '-'
| '*'
| '/';
 
puctuation: '('
|')'
|','
|';'
|'='
|'%'
|"|"
|"||"
|"&"
|"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
	/*	if (argc != 1)
	 {
	 fprintf (stderr, "Incorrect number of arguments\n");
	 exit (-1);
	 }*/
	
	if ( argc > 0 )
	{
		yyin = fopen( argv[0], "r" );
	else 
		yyin = stdin;
        yylex();
    }
	
	/*	if (!(yyin = fopen (argv[1], "r")))
	 {
	 fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	 exit (-1);
	 }*/
	yyparse();
	yydebug=1;
	
}

Open in new window

Right. You need to tell yacc/bison to generate those tokens in a header file.

If you use: yacc -d parser.y, it will create y.tab.h
If you use: bison -d parser.y, it creates parser.tab.h

Try it and take a look at the file.  You'll immediately see what to do with it to fix your lexer issues.

ok, now i get no more errors (just the multiple warnings).

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define YYDEBUG 1
 
extern yylex();
 
int yylex(void);  
 
extern FILE *yyin;
 
int yyparse(void);
int yywrap()
{
  return 1;
}
	
extern int yydebug;
 
extern void yyerror(const char *);
 
void yyerror(const char *s)
{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
}
 
%}
 
%token IDENTIFIER COMMENT VARIABLE INTEGER FLOAT STRING LITERAL UNKNOWN statement
%token PLUS MINUS TIMES SLASH LPAREN RPAREN SEMICOLON COMMA EQL OR OR2 AND AND2 
%token IF ELSE DO INT RETURN VOID T_FLOAT WHILE
 
 
 
%%
primary_expression: IDENTIFIER
					| VARIABLE
					| COMMENT
					| FLOAT
					| INTEGER
					| LITERAL
					| '(' expression ')';
 
 
keywords:	        IF
			|ELSE
			|DO
			|INT
			|RETURN 
			|VOID 
			|T_FLOAT
			|WHILE;
 
type_specifier: VOID
	        | INT
		| FLOAT;
 
ifstatement:  expression                      
			  | VARIABLE '=' expression
			  | IF '(' expression ')' statement
			  | IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
				| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
			| RETURN expression ';';
 
expression: INTEGER 
	    |
	    VARIABLE
	    |
	    expression '+' expression
	    |
	    expression '-' expression
	    |
	    expression '*' expression
	    |
	    expression '/' expression
	    | '(' expression ')';
 
operators:  '+'
	    | '-'
	    | '*'
	    | '/';
 
puctuation: '('
	    |')'
	    |','
	    |';'
	    |'='
	    |'%'
	    |"|"
	    |"||"
	    |"&"
	    |"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
 
  /*	if (argc != 1)
    {
		fprintf (stderr, "Incorrect number of arguments\n");
		exit (-1);
		}*/
 
	if ( argc > 0 )
        {
	    yyin = fopen( argv[0], "r" );
	}
	else 
	  {
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	    yyin = stdin;
        yylex();
	  }
    
	/*
		if (!(yyin = fopen (argv[1], "r")))
    {
		fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
		exit (-1);
		}*/
	yyparse();
	yydebug=1;
	
}

Open in new window

Your next step is to start giving input to your compiler.

The type of code it will parse will depend on your grammar, and your start state.

But your original problem is now solved, except if you want to now move on to using yylval you can now use %union to declare yylval.

In your yacc grammar, you override YYSTYPE with %union

%union {
   int ival;
   char * sval;
}


That will allow you to use yylval in your lexer to pass values back to your parser.

If in the you wanted to evaluate all integer constants (which are in string form in the input buffer) then you could use the ival member.

{SIGN}?{DIGIT}+ {  
   yylval.ival = atoi(yytext);
   printf("INTEGER %d", yylval.ival);
   return(INTEGER);
 }

Now that you override yylval, you have to define your tokens and types with the <> syntax to tell which member of the yylval union to use

%token <ival> INTEGER
%token <sval> VARIABLE

So when doing this, when you refer to tokens in your grammar ($1, $2, $3, etc.), yacc will use the type for the token or rule as the value. So in the following rule:

expression:  INTEGER PLUS INTEGER

$1 would be yylval.ival
why do i get that error :   ?
mygrammar.l: In function yylex:
mygrammar.l:96: error: yyval undeclared (first use in this function)
mygrammar.l:96: error: (Each undeclared identifier is reported only once
mygrammar.l:96: error: for each function it appears in.)

%{
 
#include "y.tab.h"
	
	/*
	 #define COMMENT	1
	 #define VARIABLE	2
	 #define INTEGER	3
	 #define FLOAT		4
	 #define STRING		5
	 #define T_IF		6
	 #define T_ELSE		7
	 #define T_WHILE	8
	 #define T_INT		9
	 #define T_VOID		10
	 #define T_DO		11
	 #define T_RETURN	12
	 #define T_FLOAT	13
	 #define PLUS		14
	 #define MINUS		15
	 #define TIMES		16
	 #define SLASH		17
	 #define LPAREN		18
	 #define RPAREN		19	
	 #define SEMICOLON	20
	 #define COMMA		21
	 #define EQL		22
	 #define OR		23
	 #define OR2		24
	 #define AND		25
	 #define AND2		26
	 #define LITERAL	27
	 #define IDENTIFIER	28
	 #define UNKNOWN  	29
	 */
	
%}
 
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
 
%%
 
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return IF;       }
"else"               { return ELSE;     }
"do"                 { return DO;       }
"int"                { return INT;      }
"return"             { return RETURN;   }
"void"               { return VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return WHILE;   }
 
 
{LETTER}{LETTERDIGIT}* {
	return(IDENTIFIER);
}
 
{VARIABLE}* { 
	//printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
	//strcpy(yyval.lexeme, yytext); 
	/*return T_VARIABLE*/
	return(VARIABLE);
}
 
{COMMENT} {
	return(COMMENT);
}
 
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
	// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
	return(FLOAT);
}
 
{SIGN}?{DIGIT}+ {  
	yyval.ival = atoi(yytext);
	printf("INTEGER%d",yyval.ival);
	// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
	return(INTEGER);
}
 
{STRINGCONSTANT} {
	return(LITERAL);
}
 
[ \t\n\r]            /* skip whitespace */
 
.                    { printf("Unknown character [%c]\n",yytext[0]);
return UNKNOWN;    }
 
%%
/*
 #if 0
 int main(void){
 int token;
 while(token = yylex()) {
 printf("lexed token: %d\n", token);
 }
 }
 #endif
 */
 
 
 
 
 
 
 
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> statement
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
primary_expression: IDENTIFIER
| VARIABLE
| COMMENT
| FLOAT
| INTEGER
| LITERAL
| '(' expression ')';
 
 
keywords:	        IF
|ELSE
|DO
|INT
|RETURN 
|VOID 
|T_FLOAT
|WHILE;
 
type_specifier: VOID
| INT
| FLOAT;
 
ifstatement:  expression                      
| VARIABLE '=' expression
| IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
| RETURN expression ';';
 
expression: INTEGER 
|
VARIABLE
|
expression '+' expression
|
expression '-' expression
|
expression '*' expression
|
expression '/' expression
| '(' expression ')';
 
operators:  '+'
| '-'
| '*'
| '/';
 
puctuation: '('
|')'
|','
|';'
|'='
|'%'
|"|"
|"||"
|"&"
|"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
	/*	if (argc != 1)
	 {
	 fprintf (stderr, "Incorrect number of arguments\n");
	 exit (-1);
	 }*/
	
	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	    yyin = stdin;
        yylex();
	}
    
	/*
	 if (!(yyin = fopen (argv[1], "r")))
	 {
	 fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	 exit (-1);
	 }*/
	yyparse();
	yydebug=1;
	
}

Open in new window

Maybe you aren't using the -d flag for yacc/bison? Check to make sure yylval is in y.tab.h, if not, you didn't use -d

It should look like this:

extern YYSTYPE yylval;


Im using the flag -d and yyval is in the y.tab.h.
but i still get the error :s
mygrammar.l: In function yylex:
mygrammar.l:96: error: yyval undeclared (first use in this function)
mygrammar.l:96: error: (Each undeclared identifier is reported only once
mygrammar.l:96: error: for each function it appears in.)
The problem is right in front of you. You have to pay attention to detail. :)

yyval is not yylval
ok , i tried to run the compiler with a text file but i get null .
./mycompiler /Users/mac/compiler/test.txt
Unknown character [?]

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   (null)
(null)


%{
	
#include "y.tab.h"
	
	/*
	 #define COMMENT	1
	 #define VARIABLE	2
	 #define INTEGER	3
	 #define FLOAT		4
	 #define STRING		5
	 #define T_IF		6
	 #define T_ELSE		7
	 #define T_WHILE	8
	 #define T_INT		9
	 #define T_VOID		10
	 #define T_DO		11
	 #define T_RETURN	12
	 #define T_FLOAT	13
	 #define PLUS		14
	 #define MINUS		15
	 #define TIMES		16
	 #define SLASH		17
	 #define LPAREN		18
	 #define RPAREN		19	
	 #define SEMICOLON	20
	 #define COMMA		21
	 #define EQL		22
	 #define OR		23
	 #define OR2		24
	 #define AND		25
	 #define AND2		26
	 #define LITERAL	27
	 #define IDENTIFIER	28
	 #define UNKNOWN  	29
	 */
	
%}
 
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
 
%%
 
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return IF;       }
"else"               { return ELSE;     }
"do"                 { return DO;       }
"int"                { return INT;      }
"return"             { return RETURN;   }
"void"               { return VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return WHILE;   }
 
 
{LETTER}{LETTERDIGIT}* {
	yylval.sval = atoi(yytext);
	printf("IDENTIFIER%d",yylval.sval);
	return(IDENTIFIER);
}
 
{VARIABLE}* { 
	yylval.sval = atoi(yytext);
	printf("VARIABLE%d",yylval.sval);
	return(VARIABLE);
	//printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
	//strcpy(yyval.lexeme, yytext); 
	/*return T_VARIABLE*/
}
 
{COMMENT} {
	yylval.sval = atoi(yytext);
	printf("COMMENT%d",yylval.sval);
	return(COMMENT);
}
 
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
	yylval.ival = atoi(yytext);
	printf("FLOAT%d",yylval.ival);
	// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
	// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
	return(FLOAT);
}
 
{SIGN}?{DIGIT}+ {  
	yylval.ival = atoi(yytext);
	printf("INTEGER%d",yylval.ival);
	// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
	return(INTEGER);
}
 
{STRINGCONSTANT} {
	yylval.sval = atoi(yytext);
	printf("LITERAL%d",yylval.sval);
	return(LITERAL);
}
 
[ \t\n\r]            /* skip whitespace */
 
.                    { printf("Unknown character [%c]\n",yytext[0]);
return UNKNOWN;    }
 
%%
/*
 #if 0
 int main(void){
 int token;
 while(token = yylex()) {
 printf("lexed token: %d\n", token);
 }
 }
 #endif
 */
 
 
 
 
 
 
 
 
 
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> statement
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
primary_expression: IDENTIFIER
| VARIABLE
| COMMENT
| FLOAT
| INTEGER
| LITERAL
| '(' expression ')';
 
 
keywords:	        IF
|ELSE
|DO
|INT
|RETURN 
|VOID 
|T_FLOAT
|WHILE;
 
type_specifier: VOID
| INT
| FLOAT;
 
ifstatement:  expression                      
| VARIABLE '=' expression
| IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
| RETURN expression ';';
 
expression: INTEGER 
|
VARIABLE
|
expression '+' expression
|
expression '-' expression
|
expression '*' expression
|
expression '/' expression
| '(' expression ')';
 
operators:  '+'
| '-'
| '*'
| '/';
 
puctuation: '('
|')'
|','
|';'
|'='
|'%'
|"|"
|"||"
|"&"
|"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
	/*	if (argc != 1)
	 {
	 fprintf (stderr, "Incorrect number of arguments\n");
	 exit (-1);
	 }*/
	
	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	    yyin = stdin;
        yylex();
	}
    
	/*
	 if (!(yyin = fopen (argv[1], "r")))
	 {
	 fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	 exit (-1);
	 }*/
	yyparse();
	yydebug=1;
	
}

Open in new window

Why are you using atoi() above in your {VARIABLE} rule? That was only a sample I showed. Do you know what atoi() does? It is for converting a string to an int. Its only useful for the token that parses integers:

{SIGN}?{DIGIT}+

I am concerned that you are just pasting in code that I give you without trying to understand it. Take out all of the atoi() and printf of yylval.sval except in the number rule.

Also, why are you calling yylex() directly now in your parser main? Read what I wrote in post 23934760, yyparse() is all you call from your main. That is the parser. The parser calls the lexer.
sorry about that, i just didn't know that it converts a string to integer.

i think i did the corrections but i get again null :S
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> statement
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
primary_expression: IDENTIFIER
| VARIABLE
| COMMENT
| FLOAT
| INTEGER
| LITERAL
| '(' expression ')';
 
 
keywords:	        IF
|ELSE
|DO
|INT
|RETURN 
|VOID 
|T_FLOAT
|WHILE;
 
type_specifier: VOID
| INT
| FLOAT;
 
ifstatement:  expression                      
| VARIABLE '=' expression
| IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement;
 
 
whilestatement:	WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';';
 
return_statement:	RETURN ';'
| RETURN expression ';';
 
expression: INTEGER 
|
VARIABLE
|
expression '+' expression
|
expression '-' expression
|
expression '*' expression
|
expression '/' expression
| '(' expression ')';
 
operators:  '+'
| '-'
| '*'
| '/';
 
puctuation: '('
|')'
|','
|';'
|'='
|'%'
|"|"
|"||"
|"&"
|"&&";
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
	/*	if (argc != 1)
	 {
	 fprintf (stderr, "Incorrect number of arguments\n");
	 exit (-1);
	 }*/
	
	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[0]);
	    yyin = stdin;
	}
    
	/*
	 if (!(yyin = fopen (argv[1], "r")))
	 {
	 fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	 exit (-1);
	 }*/
	yyparse();
	//	yydebug=1;
	
}
 
 
 
 
 
 
 
 
%{
	
#include "y.tab.h"
	
	/*
	 #define COMMENT	1
	 #define VARIABLE	2
	 #define INTEGER	3
	 #define FLOAT		4
	 #define STRING		5
	 #define T_IF		6
	 #define T_ELSE		7
	 #define T_WHILE	8
	 #define T_INT		9
	 #define T_VOID		10
	 #define T_DO		11
	 #define T_RETURN	12
	 #define T_FLOAT	13
	 #define PLUS		14
	 #define MINUS		15
	 #define TIMES		16
	 #define SLASH		17
	 #define LPAREN		18
	 #define RPAREN		19	
	 #define SEMICOLON	20
	 #define COMMA		21
	 #define EQL		22
	 #define OR		23
	 #define OR2		24
	 #define AND		25
	 #define AND2		26
	 #define LITERAL	27
	 #define IDENTIFIER	28
	 #define UNKNOWN  	29
	 */
	
%}
 
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
 
%%
 
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return IF;       }
"else"               { return ELSE;     }
"do"                 { return DO;       }
"int"                { return INT;      }
"return"             { return RETURN;   }
"void"               { return VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return WHILE;   }
 
 
{LETTER}{LETTERDIGIT}* {
	return(IDENTIFIER);
}
 
{VARIABLE}* { 
	return(VARIABLE);
}
 
{COMMENT} {
	return(COMMENT);
}
 
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
	yylval.ival = atoi(yytext);
	printf("FLOAT%d",yylval.ival);
	// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
	// printf("FLOAT\n");sscanf(yytext,"%d", &(yyval.value)); 
	return(FLOAT);
}
 
{SIGN}?{DIGIT}+ {  
	yylval.ival = atoi(yytext);
	printf("INTEGER%d",yylval.ival);
	// printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value)); 
	return(INTEGER);
}
 
{STRINGCONSTANT} {
	return(LITERAL);
}
 
[ \t\n\r]            /* skip whitespace */
 
.                    { printf("Unknown character [%c]\n",yytext[0]);
return UNKNOWN;    }
 
%%
/*
 #if 0
 int main(void){
 int token;
 while(token = yylex()) {
 printf("lexed token: %d\n", token);
 }
 }
 #endif
 */

Open in new window

Getting null from what? If its output from your parser, that means your parser is working, but you need to put some print statements to see what is going on.

Put some simple printf() statements in each yacc grammar rule
ok, i've got the printf statements now, but when i give a txt file as input i get null printed.

i tried commenting out the        
if ( argc > 0 )
      {
          yyin = fopen( argv[0], "r" );
      }
      else
      {
          fprintf (stderr, "Failed to open input file '%s'\n", argv[0]);
          yyin = stdin;
      }

now the parser accepts the text file which contains:
_Var
/* comment */


but i get as output :

rule 1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            [??<?????{D
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    (null)



%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> statement
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
primary_expression: IDENTIFIER {printf("rule 1\n");}
| VARIABLE {printf("rule 2\n");}
| COMMENT {printf("rule 3\n");}
| FLOAT {printf("rule 4\n");}
| INTEGER {printf("rule 5\n");}
| LITERAL {printf("rule 6\n");}
| '(' expression ')' {printf("rule 7\n");}  ;
 
 
keywords:	        IF {printf("rule 8\n");}
|ELSE {printf("rule 9\n");}
|DO {printf("rule 10\n");}
|INT {printf("rule 11\n");}
|RETURN {printf("rule 12\n");}
|VOID {printf("rule 13\n");}
|T_FLOAT {printf("rule 14\n");}
|WHILE {printf("rule 15\n");} ;
 
type_specifier: VOID {printf("rule 16\n");}
| INT {printf("rule 17\n");}
| FLOAT {printf("rule 18\n");} ;
 
ifstatement:  expression       {printf("rule 19\n");}               
| VARIABLE '=' expression {printf("rule 20\n");}
| IF '(' expression ')' statement {printf("rule 21\n");}
| IF '(' expression ')' statement ELSE statement {printf("rule 22\n");} ;
 
 
whilestatement:	WHILE '(' expression ')' statement {printf("rule 23\n");}
| DO statement WHILE '(' expression ')' ';'       {printf("rule 24\n");}  ;  
 
return_statement:	RETURN ';' {printf("rule 25\n");}
| RETURN expression ';'   {printf("rule 26\n");} ;
 
expression: INTEGER {printf("rule 27\n");}
|
VARIABLE {printf("rule 28\n");}
|
expression '+' expression  {printf("rule 29\n");}
|
expression '-' expression  {printf("rule 30\n");}
|
expression '*' expression  {printf("rule 31\n");}
|
expression '/' expression  {printf("rule 32\n");}
| '(' expression ')' {printf("rule 33\n");} ;
 
operators:  '+'  {printf("rule 34\n");}
| '-'  {printf("rule 35\n");}
| '*'  {printf("rule 36\n");}
| '/'  {printf("rule 37\n");} ; 
 
puctuation: '('  {printf("rule 38\n");}
|')' {printf("rule 39\n");}
|',' {printf("rule 40\n");}
|';' {printf("rule 41\n");}
|'=' {printf("rule 42\n");}
|'%' {printf("rule 43\n");}
|"|" {printf("rule 44\n");}
|"||" {printf("rule 45\n");}
|"&"  {printf("rule 46\n");}
|"&&" {printf("rule 47\n");} ;
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
	/*	if (argc != 1)
	 {
	 fprintf (stderr, "Incorrect number of arguments\n");
	 exit (-1);
	 }*/
	
	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[0]);
	    yyin = stdin;
	}
    
	/*
	 if (!(yyin = fopen (argv[1], "r")))
	 {
	 fprintf (stderr, "Failed to open input file '%s'\n", argv[1]);
	 exit (-1);
	 }*/
	yyparse();
	//	yydebug=1;
	
}

Open in new window

Clean up the lexer code! Takeout all of the stuff that was commented out, it is making it harder to read, for example all of the old token #defines in your lexer that no longer are needed. Clean up your main too. No need in keeping old stuff around, just remove the unused main altogether now.

Your output says, among other stuff, "rule1"

That means your parser parsed an IDENTIFIER and reduced it to a primary_expression, which is what is in your test file.

Not sure where the other stuff comes from, I will look into it if you clean it up.

This is a successful parser, as far as what you wanted to accomplish. You now have integrated lex and yacc and are using yylval. It parses and reduces a rule. I'll continue on a new question if you like, but you'll need to ask specifically what you want to accomplish in that one.



ok that's the "new" code, i'll post another question regarding the output of the compiler.

Example:

input =

_Var
/* comment */

output =

rule 1


instead of :

rule 1
rule 3
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> statement
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
primary_expression: IDENTIFIER {printf("rule 1\n");}
| VARIABLE {printf("rule 2\n");}
| COMMENT {printf("rule 3\n");}
| FLOAT {printf("rule 4\n");}
| INTEGER {printf("rule 5\n");}
| LITERAL {printf("rule 6\n");}
| '(' expression ')' {printf("rule 7\n");}  ;
 
 
keywords:	        IF {printf("rule 8\n");}
|ELSE {printf("rule 9\n");}
|DO {printf("rule 10\n");}
|INT {printf("rule 11\n");}
|RETURN {printf("rule 12\n");}
|VOID {printf("rule 13\n");}
|T_FLOAT {printf("rule 14\n");}
|WHILE {printf("rule 15\n");} ;
 
type_specifier: VOID {printf("rule 16\n");}
| INT {printf("rule 17\n");}
| FLOAT {printf("rule 18\n");} ;
 
ifstatement:  expression       {printf("rule 19\n");}               
| VARIABLE '=' expression {printf("rule 20\n");}
| IF '(' expression ')' statement {printf("rule 21\n");}
| IF '(' expression ')' statement ELSE statement {printf("rule 22\n");} ;
 
 
whilestatement:	WHILE '(' expression ')' statement {printf("rule 23\n");}
| DO statement WHILE '(' expression ')' ';'       {printf("rule 24\n");}  ;  
 
return_statement:	RETURN ';' {printf("rule 25\n");}
| RETURN expression ';'   {printf("rule 26\n");} ;
 
expression: INTEGER {printf("rule 27\n");}
|
VARIABLE {printf("rule 28\n");}
|
expression '+' expression  {printf("rule 29\n");}
|
expression '-' expression  {printf("rule 30\n");}
|
expression '*' expression  {printf("rule 31\n");}
|
expression '/' expression  {printf("rule 32\n");}
| '(' expression ')' {printf("rule 33\n");} ;
 
operators:  '+'  {printf("rule 34\n");}
| '-'  {printf("rule 35\n");}
| '*'  {printf("rule 36\n");}
| '/'  {printf("rule 37\n");} ; 
 
puctuation: '('  {printf("rule 38\n");}
|')' {printf("rule 39\n");}
|',' {printf("rule 40\n");}
|';' {printf("rule 41\n");}
|'=' {printf("rule 42\n");}
|'%' {printf("rule 43\n");}
|"|" {printf("rule 44\n");}
|"||" {printf("rule 45\n");}
|"&"  {printf("rule 46\n");}
|"&&" {printf("rule 47\n");} ;
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
 
  /*	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[0]);
	    yyin = stdin;
	}
    */
 
	yyparse();
       	yydebug=1;
	
}
 
 
 
 
 
 
 
 
%{
	
#include "y.tab.h"
	
	
%}
 
LETTER          [a-zA-Z_]
DIGIT           [0-9]
LETTERDIGIT     [a-zA-Z0-9_]
SIGN            [-+]
STRINGCONSTANT  \"[^"\n]*["\n]
CHARCONSTANT    \'[^'\n]*\'
RANKSPEC        \[[,]*\]
INTEGER		{digit}+
VARIABLE        [a-z_]({LETTERDIGIT})*
COMMENT		"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
 
%%
 
"+"                  { return PLUS;       }
"-"                  { return MINUS;      }
"*"                  { return TIMES;      }
"/"                  { return SLASH;      }
"("                  { return LPAREN;     }
")"                  { return RPAREN;     }
";"                  { return SEMICOLON;  }
","                  { return COMMA;      }
"="                  { return EQL;        }
"|"                  { return OR;         }
"||"                 { return OR2;        }
"&"                  { return AND;        }
"&&"                 { return AND2;       }
"if"                 { return IF;       }
"else"               { return ELSE;     }
"do"                 { return DO;       }
"int"                { return INT;      }
"return"             { return RETURN;   }
"void"               { return VOID;     }
"float"              { return T_FLOAT;    }
"while"              { return WHILE;   }
 
 
{LETTER}{LETTERDIGIT}* {
	return(IDENTIFIER);
}
 
{VARIABLE}* { 
	return(VARIABLE);
}
 
{COMMENT} {
	return(COMMENT);
}
 
{SIGN}?{DIGIT}+"."{DIGIT}+ {   
	yylval.ival = atoi(yytext);
	printf("FLOAT %d",yylval.ival);
	return(FLOAT);
}
 
{SIGN}?{DIGIT}+ {  
	yylval.ival = atoi(yytext);
	printf("INTEGER %d",yylval.ival);
	return(INTEGER);
}
 
{STRINGCONSTANT} {
	return(LITERAL);
}
 
[ \t\n\r]            /* skip whitespace */
 
.                    { printf("Unknown character [%c]\n",yytext[0]);
return UNKNOWN;    }
 
%%

Open in new window

1: The parser is only parsing the _Var because it reached an "accept state" because your grammar start state is primary_expression, and _Var satisfies that, so the parse is done. Your file actually includes 2 items, which isn't legal in your grammar. Your input file is

IDENTIFIER
COMMENT

Both of which reduce to primary_expression

Problem is, once it reduces the 1st one, the parse is done. I would put a full expression in your test file like:

(a + b)

That means you'll have to use the %start directive to tell the parser to use a rule other than primary_expression. Right now, your start state is primary_expression because its at the top. That is why you get all of the useless rule warnings. You should probably start with adding a statement rule that maps to expression

statement:    expression SEMICOLON
     ;

Then make your start state "statement"

%start statement



2: You don't really need to pass the COMMENT back to the parser, you can just skip it. A COMMENT is not an expression.


Change the lexer to just eat the comment without returning a token. Just put an empty rule (a semicolon should do)
For example, in my compiler, I have single line C++ style comments, here is the lex rule:

\/\/.*\n        line++;    

Note it has no return(COMMENT) so the parser never sees it as a token.


3: All of your punctuation rules are probably going to be ignored, because you have defined tokens for them in the lexer. The parser will never receive ';', it will receive SEMICOLON because that is what the lexer returns for ';'
it still does not print the tokens on  each call.

also about the comments im not sure at the moment, whether i need to treat them as tokens or not.


%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
#define YYDEBUG 1
	
	extern yylex();
	
	int yylex(void);  
	
	extern FILE *yyin;
	
	int yyparse(void);
	int yywrap()
	{
		return 1;
	}
	
	extern int yydebug;
	
	extern void yyerror(const char *);
	
	void yyerror(const char *s)
	{
        fflush(stdout);
        printf("\n%*s\n%*s\n", s);
	}
	
%}
 
%start statement
 
%union {
	int ival;
	char * sval;
}
 
%token <sval> IDENTIFIER
%token <sval> COMMENT 
%token <sval> VARIABLE
%token <ival> INTEGER
%token <ival> FLOAT
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN 
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR 
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> T_FLOAT
%token <sval> WHILE
 
 
 
 
%%
 
statement: expression SEMICOLON ;
 
primary_expression: IDENTIFIER {printf("rule 1\n");}
| VARIABLE {printf("rule 2\n");}
| COMMENT {printf("rule 3\n");}
| FLOAT {printf("rule 4\n");}
| INTEGER {printf("rule 5\n");}
| LITERAL {printf("rule 6\n");}
| '(' expression ')' {printf("rule 7\n");}  ; 
 
 
keywords:	        IF {printf("rule 8\n");}
|ELSE {printf("rule 9\n");}
|DO {printf("rule 10\n");}
|INT {printf("rule 11\n");}
|RETURN {printf("rule 12\n");}
|VOID {printf("rule 13\n");}
|T_FLOAT {printf("rule 14\n");}
|WHILE {printf("rule 15\n");} ;
 
type_specifier: VOID {printf("rule 16\n");}
| INT {printf("rule 17\n");}
| FLOAT {printf("rule 18\n");} ;
 
ifstatement:  expression       {printf("rule 19\n");}               
| VARIABLE '=' expression {printf("rule 20\n");}
| IF '(' expression ')' statement {printf("rule 21\n");}
| IF '(' expression ')' statement ELSE statement {printf("rule 22\n");} ;
 
 
whilestatement:	WHILE '(' expression ')' statement {printf("rule 23\n");}
| DO statement WHILE '(' expression ')' ';'       {printf("rule 24\n");}  ;  
 
return_statement:	RETURN ';' {printf("rule 25\n");}
| RETURN expression ';'   {printf("rule 26\n");} ;
 
expression: INTEGER {printf("rule 27\n");}
|
VARIABLE {printf("rule 28\n");}
|
expression '+' expression  {printf("rule 29\n");}
|
expression '-' expression  {printf("rule 30\n");}
|
expression '*' expression  {printf("rule 31\n");}
|
expression '/' expression  {printf("rule 32\n");}
| '(' expression ')' {printf("rule 33\n");} ;
 
operators:  '+'  {printf("rule 34\n");}
| '-'  {printf("rule 35\n");}
| '*'  {printf("rule 36\n");}
| '/'  {printf("rule 37\n");} ; 
 
puctuation: '('  {printf("rule 38\n");}
|')' {printf("rule 39\n");}
|',' {printf("rule 40\n");}
|';' {printf("rule 41\n");}
|'=' {printf("rule 42\n");}
|'%' {printf("rule 43\n");}
|"|" {printf("rule 44\n");}
|"||" {printf("rule 45\n");}
|"&"  {printf("rule 46\n");}
|"&&" {printf("rule 47\n");} ;
 
 
%%
 
 
 
int main (int argc, char *argv[])
{
	
 
  /*	if ( argc > 0 )
	{
	    yyin = fopen( argv[0], "r" );
	}
	else 
	{
	    fprintf (stderr, "Failed to open input file '%s'\n", argv[0]);
	    yyin = stdin;
	}
    */
 
	yyparse();
       	yydebug=1;
	
}
 
 
 
 
 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey,

Can yo please have a look to that question : https://www.experts-exchange.com/questions/24279119/print-parse-tree.html

Thanks in advance !