Yacc rogram


 
Thread Tools Search this Thread
Top Forums Programming Yacc rogram
# 1  
Old 12-21-2012
Yacc rogram

hello, actually i want to generate rgram in yacc able to do do several operations separated by ';' and after show all the results separated by ';' again.
My code do just one operations and i couldn' t find solution to do several ones separated by ';'. I thought may be i can do a do... while but i don't know how to do it for my example. If someone can help me please.
here is my .y for just one operation

Code:
%{ double vbltable[26]; %}

%union {
       double dval ;
       int vblno; }

%token <vblno> NAME 
%token <dval>  NUMBER

%type  <dval> expression

%%
statement_list:  
  statement '\n'
| statement_list statement '\n'
;

statement: 
  NAME '=' expression { vbltable[$1] = $3; }
| expression          { printf("est %g\n", $1); }
;

expression: 
  expression expression '+' { $$ = $1 + $2; }
| expression expression '-' { $$ = $1 - $2; }
| expression expression '*' { $$ = $1 * $2; }
| expression expression '/' {if($2 == 0.0)
                             printf("erreur ! Division par 0");
                             else
                             $$ = $1 / $2; }
| '(' '-' expression ')'    { $$ = -$3; }
| NUMBER                    { $$ = $1; }
| NAME                      { $$ = vbltable[$1]; }
;
%%

int yyerror() { printf("erreur") ; } 
int main () { yyparse(); }

and here is my .l

Code:
%{ 
#include "y.tab.h" 
#include <math.h>
extern double vbltable[26];
%}

%%

([0-9]+|([0-9]*"."[0-9]+)) { yylval.dval = atof(yytext); return NUMBER; }
\t;                        { ; }
" "                        { ; }
[a-z]                      { yylval.vblno = yytext[0] - 'a' ; return NAME;}
"$"                        { return 0; 
                             /* symb. de retour pour fin de fichier */}
\n                         { return yytext[0]; }
.                          { return yytext[0]; }

%%

thanks

---------- Post updated at 03:14 PM ---------- Previous update was at 09:06 AM ----------

no answer?? come on it's christmas soon Smilie

Last edited by joeyg; 12-21-2012 at 10:08 AM.. Reason: Please wrap commands and data with CodeTags
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

Problem with Yacc

Hi guys, I'm struggling with a simple but at the same time complicated problem related to yacc :wall: Maybe someone can help me with this :confused: I'm working on a big project and it's hard for me to show the problem exactly on my code, but I will give a model of it. Before I had this kind... (2 Replies)
Discussion started by: sisi
2 Replies

2. Programming

parsing fixed length field with yacc/bison

How to specify the token length in a yacc file? sample input format <field1,data type ans,fixed length 6> followed by <field2,data type ans,fixed length 3> Example i/p and o/p Sample Input: "ab* d2 9o" O/p : "Field1 Field2 " yacc/bison grammar: record :... (1 Reply)
Discussion started by: sungita
1 Replies

3. Programming

actions before parsing rules in lex n yacc

Hi , We have developed a grammer for our domain language using lex n yacc. I want to know is there any pre defined lex-yacc function which gets call before executing any rule (or rules). Oue requirement is, before processing any rule ,we want to perform some specific actions ? is there... (0 Replies)
Discussion started by: supritjain
0 Replies

4. Programming

How to match n number of newline character(\n) in lex and yacc

Hi , I need to develop a parser which should match something like 1. text a=5 " a=20"; 2. text a=..." a=20"; 3 text a=..." a=20 b=34 c=12 "; I have used this regular expression in my Lex file to generate the tokens: \".\s*.*\s.\" (8 Replies)
Discussion started by: vishwa787
8 Replies
Login or Register to Ask a Question