File: MyParser.y

package info (click to toggle)
bison%2B%2B 1.21.11-3.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,628 kB
  • ctags: 882
  • sloc: cpp: 8,198; sh: 613; ansic: 464; makefile: 112; yacc: 44; lex: 30
file content (55 lines) | stat: -rw-r--r-- 1,141 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%{
#define YY_MyParser_STYPE yy_MyParser_stype
%}
%name MyParser
%define LSP_NEEDED
%define ERROR_BODY =0
%define LEX_BODY =0
%header{
#include <iostream>
#include <string>
  using namespace std;
#define YY_DECL int yyFlexLexer::yylex(YY_MyParser_STYPE *val)
#ifndef FLEXFIX
#define FLEXFIX YY_MyParser_STYPE *val
#define FLEXFIX2 val
#endif
%}

%union {
  int num;
  bool statement;
  }



%token <num> PLUS INTEGER MINUS  AND OR NOT LPARA RPARA
%token <statement> BOOLEAN
%type <num> exp result
%type <statement> bexp
%start result

%left OR
%left AND
%left PLUS MINUS
%left NOT
%left LPARA RPARA

%%

result          : exp {cout << "Result = " << $1 << endl;}
                | bexp {cout << "Result = " << $1 << endl;}

exp		: exp PLUS exp {$$ = $1 + $3;}
       		| INTEGER {$$ = $1;}
                | MINUS exp { $$ = -$2;}
                | exp MINUS exp {$$ = $1 - $3;}

bexp            : BOOLEAN {$$ = $1;}
		| bexp AND bexp { $$ = $1 && $3;}
		| bexp OR bexp { $$ = $1 || $3;}
		| NOT bexp {$$ = !$2;}
		| LPARA bexp RPARA {$$ = $2}
%%
/* -------------- body section -------------- */
// feel free to add your own C/C++ code here