52 lines
1006 B
Plaintext
52 lines
1006 B
Plaintext
|
%{
|
||
|
#include <stdio.h>
|
||
|
int yylex();
|
||
|
int yyerror(const char* p) { fprintf(stderr, p); }
|
||
|
%}
|
||
|
|
||
|
%define parse.trace
|
||
|
|
||
|
%union {
|
||
|
char* sym;
|
||
|
float num;
|
||
|
}
|
||
|
|
||
|
%token <sym> ID
|
||
|
%token STOP
|
||
|
%token <num> NUM
|
||
|
%token STRING
|
||
|
%token INDENT DEDENT
|
||
|
%token IF ELIF ELSE
|
||
|
|
||
|
%right GETS
|
||
|
%left '+' '-'
|
||
|
%left '/' '*'
|
||
|
%left MAPS
|
||
|
|
||
|
%%
|
||
|
program: statements | statements statement;
|
||
|
statements: statements statement STOP
|
||
|
| statements STOP
|
||
|
| // null production ;
|
||
|
statement: assignment | funcall | conditional;
|
||
|
assignment: ID GETS expr;
|
||
|
expr: funcdef
|
||
|
| funcall
|
||
|
| expr '+' expr
|
||
|
| expr '-' expr
|
||
|
| expr '*' expr
|
||
|
| expr '/' expr
|
||
|
| '(' expr ')'
|
||
|
| ID | NUM | STRING;
|
||
|
|
||
|
funcdef: param MAPS expr | param MAPS block;
|
||
|
param: ID | '_' | // null production;
|
||
|
funcall: ID '(' exprlist ')'
|
||
|
exprlist: exprlist ',' expr | expr | // null production;
|
||
|
|
||
|
conditional: IF expr block elifs
|
||
|
| IF expr block elifs ELSE block;
|
||
|
elifs: ELIF expr block elifs| // null production;
|
||
|
|
||
|
block: STOP INDENT statement statements DEDENT
|
||
|
%%
|