For Programmers: Free Programming Magazines  


Home > Archive > Compilers > June 2007 > Yacc question related to %type









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Yacc question related to %type
g_p_f_e

2007-06-01, 2:14 pm

Hi to all!

I have made a yacc program example.y

%{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* #include "parser.h" */

%}

%start program

%union {
char *string;
int integer;
}


%token <string> ID
%token <integer> NUMBER
%token <string> STRING
%token IF ELSE WHILE FOR
%token FUNCTION RETURN
%token TRUE FALSE NIL AND NOT OR

%left '='
%left "||"
%left "&&"
%nonassoc "==" "!="
%nonassoc '>' ">=" '<' "<="
%left '+' '-'
%left '*' '/' '%'
%right '!' "++" "--" UMINUS
%left '.'

%%

program: statements
;

statements: statements stmt
|
;

stmt:
';'
|expr ';'
|whilestmt
|forstmt
|returnstmt
|block
|funcdef
|ifstmt
;

expression:
assignexpr
|expr '+' expr { $$ = $1 + $3;} /* HERE IS THE PROBLEM */
|expr '-' expr
|expr '*' expr
|expr '/' expr /*{ if($3==0)
yyerror(.divide 0.);
else
$$ = $1 / $3;} */
|expr '%' expr
|expr '>' expr
|expr ">=" expr
|expr '<' expr
|expr "<=" expr
|expr "==" expr
|expr "!=" expr
|expr "&&" expr
|expr "||" expr
;


expr: expression
;
%%


int main() {

}



Well in the example.y file we have the terminal symbols and non-terminal symbols
The terminal symbols i use are NUMBER,ID,STRING i have defined a type for them
e.g. ID has a string value, NUMBER has an integer value etc
Well i'm as far as the %type is concerned, We have to give a type to non terminals too.
If we do yacc example.y we take the output:

hello.y:58.36-37: $$ of `expression' has no declared type
hello.y:58.41-42: $1 of `expression' has no declared type
hello.y:58.46-47: $3 of `expression' has no declared type

This happens cause expression which is a non-terminal symbol has no type!

What type can this symbol take?shall i define a struct for this?

If i do : %type <integer> expression i take :

hello.y:61.18-30: warning: type clash on default action: <integer> != <>
hello.y:62.18-30: warning: type clash on default action: <integer> != <>
hello.y:63.18-30: warning: type clash on default action: <integer> != <>
hello.y:67.18-30: warning: type clash on default action: <integer> != <>
hello.y:68.18-30: warning: type clash on default action: <integer> != <>
hello.y:69.18-31: warning: type clash on default action: <integer> != <>
hello.y:70.18-30: warning: type clash on default action: <integer> != <>
hello.y:71.18-31: warning: type clash on default action: <integer> != <>
hello.y:72.18-31: warning: type clash on default action: <integer> != <>
hello.y:73.18-31: warning: type clash on default action: <integer> != <>
hello.y:74.18-31: warning: type clash on default action: <integer> != <>
hello.y:75.18-31: warning: type clash on default action: <integer> != <>

I would be glad if someone could help me!

Thanks in advance!!!
Adencejo

2007-06-12, 10:00 am

Helen Hunt and Carmen Electra Tormented By Cowgirls!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com