summaryrefslogtreecommitdiff
path: root/grammar.y
blob: a37dae66a43d324563bfff7d14f5133ffe441c84 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%{
#include <stdio.h>

#include "ast.c"

extern FILE *yyin;

extern int yylex();
extern void yyerror();

ast_node* yyast = NULL;
%}

%define parse.error verbose

%union {
  char* string;
  int number;
  ast_node* node;
}

%token OPERATOR_PLUS
%token OPERATOR_MINUS
%token <string> ASSIGN

%token FUNCTION
%token IF
%token ELSE
%token RETURN
%token <string> CONST
%token VAR

%token <string> TYPE_INTEGER
%token <string> TYPE_STRING
%token <string> TYPE_VOID

%token PARENTHESIS_LEFT
%token PARENTHESIS_RIGHT
%token BRACE_LEFT
%token BRACE_RIGHT
%token BRACKET_LEFT
%token BRACKET_RIGHT

%token COMMA;
%token <string> COLON;

%token <number> NUMBER;
%token <string> IDENTIFIER
%token COMMENT;

%token END_OF_LINE;

%type <node> program
%type <node> function
%type <node> arguments
%type <node> argument
%type <node> return_type
%type <node> variable_type
%type <node> statements
%type <node> statement
%type <node> expression

%start program

%%
program:
  statement END_OF_LINE
  | program program { $$ = create_program_node($1, $2); }

variable_type:
  TYPE_INTEGER { printf("variable_type:: %s\n", $1); $$ = create_type_node($1); }
  | TYPE_STRING { $$ = create_type_node($1); }


statements:
  statement
  | statements END_OF_LINE statement

statement:
  // const henshin: integer = 2
  CONST IDENTIFIER COLON variable_type ASSIGN expression { printf("$1: %s -- $2: %s -- $3: %s -- $4: %s -- $5: %s -- $6: %s\n", $1, $2, $3, "4", $5, "6"); }


expression:
  NUMBER
%%

void main (int argc, char **argv)
{
  #ifdef YYDEBUG
    yydebug = 1;
  #endif

  //henshin_lex();
  yyin = fopen(argv[1], "r");
  yyparse();

  printf("%s", yyast);
}