initial import

git-svn-id: svn://anubis/jtlc/trunk@2 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh committed 2009-03-28 15:17:48 +00:00
1 parent 8bfe25effa
commit 01bd481bce
11 files changed
+432

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
FLEX := flex
BISON := bison
PARSER := parser
OBJS := lex.yy.o $(PARSER).tab.o $(patsubst %.cc,%.o,$(wildcard *.cc))
DEPS := $(OBJS:.o=.dep)
all: $(DEPS) $(OBJS)
%.o: %.cc
$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<
$(PARSER).tab.cc $(PARSER).tab.hh: $(PARSER).yy
$(BISON) -d $<
lex.yy.o: lex.yy.cc
lex.yy.cc: $(PARSER).tab.hh
lex.yy.cc: $(PARSER).lex
$(FLEX) -o $@ $<
# Make dependency files
%.dep: %.cc
@set -e; rm -f $@; \
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
clean:
-rm -f lex.yy.cc $(PARSER).tab.cc $(PARSER).tab.hh *~ *.o *.dep
# Include dependency files
ifndef CLEAN
-include $(DEPS)
endif
+85
View File
@@ -0,0 +1,85 @@
%option nounput
%option bison-locations
%{
#include "parser.tab.hh"
%}
%%
/* operators */
:= return ASSIGN;
== return DEQUALS;
\/ return DIVIDE;
= return EQUALS;
\> return GREATER;
\< return LESS;
=~ return MATCH;
- return MINUS;
% return MOD;
\+ return PLUS;
\* return STAR;
/* punctuation */
: return COLON;
, return COMMA;
:: return DCOLON;
\$ return DOLLAR;
\. return DOT;
\<- return FROM;
\? return QUESTION;
; return SEMICOLON;
\{ return LCURLY;
\} return RCURLY;
\[ return LBRACKET;
\] return RBRACKET;
\( return LPAREN;
\) return RPAREN;
/* literals */
-?[0-9]+ {
return INT_LITERAL;
}
-?0x[0-9]+ {
return INT_LITERAL;
}
-?0b[01]+ {
return INT_LITERAL;
}
-?[0-9]*\.[0-9]+([eE][0-9]+)? {
return REAL_LITERAL;
}
/* primitive types */
byte return BYTE;
ubyte return UBYTE;
char return CHAR;
wchar return WCHAR;
int return INT;
uint return UINT;
long return LONG;
ulong return ULONG;
/* keywords */
import return IMPORT;
module return MODULE;
return return RETURN;
struct return STRUCT;
/* identifiers */
[a-zA-Z_][a-zA-Z_0-9]* {
return IDENTIFIER;
}
/* whitespace */
\n yylloc->first_line++; yylloc->last_line++;
[ \t\v] /* ignore whitespace */
/* anything else */
. return yytext[0];
%%
+130
View File
@@ -0,0 +1,130 @@
%{
#include <stdio.h>
#include <iostream>
#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;
}
%}
%pure-parser
%locations
%error-verbose
/* operators */
%token ASSIGN;
%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;
/* primitive types */
%token BYTE;
%token UBYTE;
%token CHAR;
%token WCHAR;
%token INT;
%token UINT;
%token LONG;
%token ULONG;
/* keywords */
%token IMPORT;
%token MODULE;
%token RETURN;
%token STRUCT;
/* identifiers */
%token IDENTIFIER;
%%
program: INT_LITERAL
;
primitive_type: BYTE
| UBYTE
| CHAR
| WCHAR
| INT
| UINT
| LONG
| ULONG
;
struct_type: STRUCT
;
ptr_type: type STAR
;
type: primitive_type
| ptr_type
| struct_type
;
%%
void parse(const char * fileName)
{
yyin = fopen(fileName, "r");
if (yyin == NULL)
{
cerr << "Failed to open file '" << fileName << "'" << endl;
return;
}
if (yyparse())
{
cerr << "Aborting." << endl;
exit(1);
}
}
void errFunc(const char * str, YYLTYPE * yyllocp)
{
fprintf(stderr, "error: %s: line %d, column %d\n",
str,
yyllocp->first_line,
yyllocp->first_column);
}