jtlc/parser/parser.yy
josh 55b80e8f09 started filling in new node types, creating them in parser
git-svn-id: svn://anubis/jtlc/trunk@13 f5bc74b8-7b62-4e90-9214-7121d538519f
2010-01-13 20:48:37 +00:00

239 lines
4.1 KiB
Plaintext

%{
#include <stdio.h>
#include <iostream>
#include "parser.h"
#include "parser.tab.hh" /* bison-generated header with YY[SL]TYPE */
using namespace std;
#define yyerror(msg) errFunc(msg, &yylloc)
int yylex(YYSTYPE *, YYLTYPE *);
extern FILE * yyin;
void errFunc(const char * str, YYLTYPE * yyllocp);
int yywrap()
{
return 1;
}
static YYSTYPE parse_result;
%}
%pure-parser
%locations
%error-verbose
/* operators */
%token ASSIGN;
%token DASSIGN;
%token DEQUALS;
%token DIVIDE;
%token EQUALS;
%token GREATER;
%token LESS;
%token MATCH;
%token MINUS;
%token MOD;
%token PLUS;
%token STAR;
/* punctuation */
%token COLON;
%token COMMA;
%token DCOLON;
%token DOLLAR;
%token DOT;
%token FROM;
%token QUESTION;
%token SEMICOLON;
%token LCURLY;
%token RCURLY;
%token LBRACKET;
%token RBRACKET;
%token LPAREN;
%token RPAREN;
/* literals */
%token INT_LITERAL;
%token REAL_LITERAL;
%token STRING_LITERAL;
/* primitive types */
%token BYTE;
%token UBYTE;
%token CHAR;
%token WCHAR;
%token SHORT;
%token USHORT;
%token INT;
%token UINT;
%token LONG;
%token ULONG;
%token FLOAT;
%token DOUBLE;
/* keywords */
%token C;
%token IMPORT;
%token MODULE;
%token RETURN;
%token STRUCT;
/* identifiers */
%token IDENTIFIER;
%%
program: program_items {
$$ = new ProgramNode();
$$->addChildren($1);
}
;
program_items: /* empty */
| program_item program_items
;
program_item: module_declaration
| import
| variable_declaration
| function
| c_statement
;
module_declaration: MODULE module_name SEMICOLON
;
module_name: IDENTIFIER module_name_more
;
module_name_more: /* empty */
| DOT IDENTIFIER module_name_more
;
import: IMPORT import_name SEMICOLON
;
import_name: IDENTIFIER import_name_more
;
import_name_more: /* empty */
| DOT import_name_more_more
;
import_name_more_more: IDENTIFIER import_name_more
| STAR
;
function: IDENTIFIER LPAREN parameter_list RPAREN COLON type LCURLY function_items RCURLY
;
function_items: /* empty */
| statement function_items
;
parameter_list: /* empty */
| variable_spec parameter_list_more
;
parameter_list_more: /* empty */
| COMMA variable_spec parameter_list_more
;
primitive_type: BYTE
| UBYTE
| CHAR
| WCHAR
| SHORT
| USHORT
| INT
| UINT
| LONG
| ULONG
;
struct_type: STRUCT LCURLY struct_items RCURLY
;
struct_items: /* empty */
| struct_item struct_items
;
struct_item: variable_declaration
;
variable_declaration: variable_spec SEMICOLON;
;
variable_spec: IDENTIFIER COLON type
;
ptr_type: type STAR
;
array_type: type LBRACKET RBRACKET
;
type: primitive_type
| ptr_type
| struct_type
| array_type
| IDENTIFIER
;
statement: expression SEMICOLON
| return_statement
| c_statement {
$$ = $1;
}
;
return_statement: RETURN expression SEMICOLON
;
expression: assign_expr
;
assign_expr: lvalue ASSIGN expression
| lvalue DASSIGN expression
;
lvalue: IDENTIFIER
;
c_statement: C LPAREN STRING_LITERAL RPAREN SEMICOLON {
$$ = new CStatementNode($3->getString());
}
;
%%
YYSTYPE parse(const char * fileName)
{
yyin = fopen(fileName, "r");
if (yyin == NULL)
{
cerr << "Failed to open file '" << fileName << "'" << endl;
}
else if (yyparse())
{
cerr << "Aborting." << endl;
exit(1);
}
return parse_result;
}
void errFunc(const char * str, YYLTYPE * yyllocp)
{
fprintf(stderr, "error: %s: line %d, column %d\n",
str,
yyllocp->first_line,
yyllocp->first_column);
}