154 lines
2.9 KiB
Plaintext
154 lines
2.9 KiB
Plaintext
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void yyerror(const char *s);
|
|
|
|
typedef struct ASTNode {
|
|
char *type;
|
|
char *value;
|
|
struct ASTNode *left;
|
|
struct ASTNode *right;
|
|
} ASTNode;
|
|
|
|
|
|
|
|
ASTNode *createNode(const char *type, const char *value, ASTNode *left, ASTNode *right);
|
|
%}
|
|
|
|
%token SIMPLETYPE CLASS ENTRY IF WHILE WRITE READ RETURN
|
|
%token IDENTIFIER
|
|
%token CONSTANTEXP
|
|
%token LBRACE RBRACE LPAREN RPAREN LBRACKET RBRACKET ASSIGN EQ LT LE GT GE SEMICOLON DOT PLUS MINUS MULT DIV
|
|
%token COMMA
|
|
|
|
%start program
|
|
|
|
%left PLUS MINUS
|
|
%left MULT DIV
|
|
%left LT LE GT GE EQ
|
|
|
|
%%
|
|
|
|
program:
|
|
CLASS IDENTIFIER LBRACE entry_declaration class_body RBRACE
|
|
;
|
|
|
|
|
|
class_body:
|
|
%empty
|
|
| atrib_declaration class_body
|
|
| method_declaration class_body
|
|
;
|
|
atrib_declaration:
|
|
SIMPLETYPE IDENTIFIER SEMICOLON
|
|
| SIMPLETYPE IDENTIFIER ASSIGN CONSTANTEXP SEMICOLON
|
|
;
|
|
|
|
method_declaration:
|
|
SIMPLETYPE IDENTIFIER LPAREN method_params RPAREN LBRACE method_body RBRACE
|
|
;
|
|
|
|
method_params:
|
|
%empty
|
|
| SIMPLETYPE IDENTIFIER
|
|
| SIMPLETYPE IDENTIFIER COMMA method_params
|
|
;
|
|
|
|
method_body:
|
|
%empty
|
|
| statement_list
|
|
;
|
|
|
|
statement_list:
|
|
%empty
|
|
| statement statement_list
|
|
;
|
|
|
|
statement:
|
|
atrib_statement
|
|
| if_statement
|
|
| while_statement
|
|
| write_statement
|
|
| read_statement
|
|
| return_statement
|
|
;
|
|
|
|
atrib_statement:
|
|
IDENTIFIER ASSIGN expression SEMICOLON
|
|
;
|
|
|
|
if_statement:
|
|
IF LPAREN expression RPAREN LBRACE statement_list RBRACE
|
|
;
|
|
|
|
while_statement:
|
|
WHILE LPAREN expression RPAREN LBRACE statement_list RBRACE
|
|
;
|
|
|
|
write_statement:
|
|
WRITE LPAREN expression RPAREN SEMICOLON
|
|
;
|
|
|
|
read_statement:
|
|
READ LPAREN IDENTIFIER RPAREN SEMICOLON
|
|
;
|
|
|
|
return_statement:
|
|
RETURN expression SEMICOLON
|
|
;
|
|
|
|
expression:
|
|
CONSTANTEXP
|
|
| expression PLUS expression
|
|
| expression MINUS expression
|
|
| expression MULT expression
|
|
| expression DIV expression
|
|
| expression EQ expression
|
|
| expression LT expression
|
|
| expression LE expression
|
|
| expression GT expression
|
|
| expression GE expression
|
|
| LPAREN expression RPAREN
|
|
| IDENTIFIER
|
|
| IDENTIFIER LBRACKET expression RBRACKET
|
|
| LBRACKET array_list RBRACKET
|
|
| class_method_call
|
|
| class_expression
|
|
;
|
|
|
|
class_expression:
|
|
IDENTIFIER
|
|
| IDENTIFIER DOT class_expression
|
|
;
|
|
|
|
class_method_call:
|
|
class_expression LPAREN array_list RPAREN
|
|
|
|
array_list:
|
|
%empty
|
|
| expression
|
|
| expression COMMA array_list
|
|
;
|
|
|
|
entry_declaration:
|
|
SIMPLETYPE ENTRY IDENTIFIER LPAREN RPAREN LBRACE statement_list RBRACE
|
|
;
|
|
|
|
|
|
%%
|
|
|
|
void yyerror(const char *s) {
|
|
fprintf(stderr, "Error: %s\n", s);
|
|
}
|
|
|
|
ASTNode *createNode(const char *type, const char *value, ASTNode *left, ASTNode *right) {
|
|
ASTNode *node = (ASTNode *)malloc(sizeof(ASTNode));
|
|
node->type = strdup(type);
|
|
node->value = value ? strdup(value) : NULL;
|
|
node->left = left;
|
|
node->right = right;
|
|
return node;
|
|
}
|