Assignment-2/parser.y

139 lines
1.6 KiB
Plaintext

%{
#include <string>
#include <iostream>
#include <set>
#include "parser.hpp"
%}
%token INDENT
%token DEDENT
%token NEWLINE
%token AND
%token BREAK
%token DEF
%token ELIF
%token ELSE
%token FOR
%token IF
%token NOT
%token OR
%token RETURN
%token WHILE
%token BOOLEAN
%token FLOAT
%token INTEGER
%token IDENTIFIER
%token ASSIGN
%token PLUS
%token MINUS
%token TIMES
%token DIVIDEDBY
%token EQ
%token NEQ
%token GT
%token GTE
%token LT
%token LTE
%token LPAREN
%token RPAREN
%token COMMA
%token COLON
%define api.value.type { const char* }
%define api.pure full
%define api.push-pull push
%start program
%{
void yyerror(const char* s) {
std::cout << s << std::endl;
}
std::set<std::string> varset;
%}
%%
program
: stmts
;
stmts
: stmts stmt
| stmt
;
stmt
: expr NEWLINE
| if
| while
| BREAK NEWLINE
;
expr
: assign
| eq
;
while
: WHILE expr COLON NEWLINE block
;
if
: IF expr COLON NEWLINE block ifend
| IF expr COLON NEWLINE block
;
ifend
: ELIF expr COLON NEWLINE block ifend
| ELIF expr COLON NEWLINE block
| ELSE COLON NEWLINE block
block
: INDENT stmts DEDENT
;
assign
: IDENTIFIER ASSIGN expr
;
eq
: eq EQ rel
| eq NEQ rel
| rel
;
rel
: rel LT sum
| rel LTE sum
| rel GT sum
| rel GTE sum
| sum
;
sum
: sum PLUS factor
| sum MINUS factor
| factor
;
factor
: factor TIMES term
| factor DIVIDEDBY term
| term
;
term
: IDENTIFIER
| FLOAT
| INTEGER
| BOOLEAN
| LPAREN expr RPAREN
;
%%