/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/parsers/test/parser.y

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
    #include <stdio.h>
 
3
    #include <stdlib.h>
 
4
    #include <stdarg.h>
 
5
 
 
6
    #include "parser_vars.hpp"
 
7
    
 
8
    int  yylex ();
 
9
 
 
10
    void yyerror(const char *str);
 
11
    void debug(const char *format, ...);
 
12
    
 
13
    Variables vars;
 
14
%}
 
15
 
 
16
%token NUM VAR END
 
17
%right '='
 
18
%left '-' '+'
 
19
%left '*' '/'
 
20
%token '(' ')'
 
21
 
 
22
%union {
 
23
    double val;
 
24
    char *var;
 
25
}
 
26
 
 
27
%%
 
28
 
 
29
app: /* empty */
 
30
    | opseq
 
31
    | opseq END
 
32
    ;
 
33
    
 
34
opseq: op
 
35
    | opseq END op
 
36
    ;
 
37
 
 
38
op: expseq { printf("Result: %lf\n", $<val>1); }
 
39
    | VAR '=' expseq { 
 
40
        debug("=   %s %lf\n", $<var>1, $<val>3);
 
41
        vars.Set($<var>1, $<val>3);
 
42
        free($<var>1);
 
43
    }
 
44
    ;
 
45
 
 
46
expseq: NUM { debug("NUM %lf\n", $<val>1); }
 
47
    | VAR { 
 
48
        $<val>$ = vars.Get($<var>1);
 
49
        debug("VAR %s\n", $<var>1);
 
50
        free($<var>1);
 
51
    }
 
52
    | expseq '+' expseq { $<val>$ = $<val>1 + $<val>3;  debug("+   %lf %lf\n", $<val>1, $<val>3); }
 
53
    | expseq '-' expseq { $<val>$ = $<val>1 - $<val>3;  debug("-   %lf %lf\n", $<val>1, $<val>3);  }
 
54
    | expseq '*' expseq { $<val>$ = $<val>1 * $<val>3;  debug("*   %lf %lf\n", $<val>1, $<val>3);  }
 
55
    | expseq '/' expseq { $<val>$ = $<val>1 / $<val>3;  debug("/   %lf %lf\n", $<val>1, $<val>3);  }
 
56
    | '(' expseq ')' { $<val>$ = $<val>2; debug("()  %lf\n", $<val>2); }
 
57
    ;
 
58
 
 
59
    
 
60
%%
 
61
 
 
62
 
 
63
void debug(const char *format, ...) {
 
64
/*
 
65
    va_list ap;
 
66
    
 
67
    va_start(ap, format);
 
68
    vprintf(format, ap);
 
69
    va_end(ap);
 
70
*/
 
71
}
 
72
 
 
73
void yyerror(const char *str) {
 
74
    fprintf(stderr, "error parsing input file: '%s'\n", str);
 
75
}
 
76
 
 
77
main() {
 
78
    
 
79
    yyparse();
 
80
}