Commit f5cf7cdc authored by zygzagZ's avatar zygzagZ

init

parents
*.o
*.l
*.y
/lat/
/TestGrammar
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
position token PIdent (letter (letter|digit|'_'|'\'')*) ;
-- programs ------------------------------------------------
entrypoints Program ;
Prog. Program ::= [TopDef] ;
FnDef. TopDef ::= FunDef ;
ClDef. TopDef ::= ClassDef ;
separator nonempty TopDef "" ;
-- function def --------------------------------------------
FuncDef. FunDef ::= Type PIdent "(" [Arg] ")" Block ;
Ar. Arg ::= Type PIdent;
separator Arg "," ;
-- class def -----------------------------------------------
ClassDefN. ClassDef ::= "class" PIdent ClassBlock ;
ClassDefE. ClassDef ::= "class" PIdent "extends" PIdent ClassBlock ;
ClassBl. ClassBlock ::= "{" [ClassBlockDef] "}" ;
ClassMthd. ClassBlockDef ::= FunDef ;
ClassFld. ClassBlockDef ::= Type [Item] ";" ;
separator ClassBlockDef "" ;
-- statements ----------------------------------------------
Blk. Block ::= "{" [Stmt] "}" ;
separator Stmt "" ;
Empty. Stmt ::= ";" ;
BStmt. Stmt ::= Block ;
Decl. Stmt ::= Type [Item] ";" ;
NoInit. Item ::= PIdent ;
Init. Item ::= PIdent "=" Expr ;
separator nonempty Item "," ;
Ass. Stmt ::= Expr "=" Expr ";" ;
TableAss. Stmt ::= PIdent "[" Expr "]" "=" Expr ";" ;
TableIncr. Stmt ::= PIdent "[" Expr "]" "++" ";" ;
TableDecr. Stmt ::= PIdent "[" Expr "]" "--" ";" ;
Incr. Stmt ::= PIdent "++" ";" ;
Decr. Stmt ::= PIdent "--" ";" ;
Ret. Stmt ::= "return" Expr ";" ;
VRet. Stmt ::= "return" ";" ;
Cond. Stmt ::= "if" "(" Expr ")" Stmt ;
CondElse. Stmt ::= "if" "(" Expr ")" Stmt "else" Stmt ;
While. Stmt ::= "while" "(" Expr ")" Stmt ;
SExp. Stmt ::= Expr ";" ;
ForEach. Stmt ::= "for" "(" Type PIdent ":" Expr ")" Stmt ;
-- Types ---------------------------------------------------
Int. Type ::= "int" ;
Str. Type ::= "string" ;
Bool. Type ::= "boolean" ;
Void. Type ::= "void" ;
Array. Type ::= Type "[" "]" ;
ClassT. Type ::= PIdent ;
internal Fun. Type ::= Type "(" [Type] ")" ;
separator Type "," ;
-- Expressions ---------------------------------------------
EVar. Expr6 ::= PIdent ;
ELitInt. Expr6 ::= Integer ;
ELitTrue. Expr6 ::= "true" ;
ELitFalse. Expr6 ::= "false" ;
EApp. Expr6 ::= PIdent "(" [Expr] ")" ;
EString. Expr6 ::= String ;
ENewArray. Expr6 ::= "new" Type "[" Expr "]" ;
ENewClass. Expr6 ::= "new" PIdent ;
EClsMmbr. Expr6 ::= Expr6 "." PIdent ;
EClsMthd. Expr6 ::= Expr6 "." PIdent "(" [Expr] ")" ;
Null. Expr6 ::= "null" ;
EIndexAcc. Expr6 ::= PIdent "[" Expr "]" ;
ECast. Expr5 ::= "(" PIdent ")" Expr5 ;
Neg. Expr5 ::= "-" Expr6 ;
Not. Expr5 ::= "!" Expr6 ;
EMul. Expr4 ::= Expr4 MulOp Expr5 ;
EAdd. Expr3 ::= Expr3 AddOp Expr4 ;
ERel. Expr2 ::= Expr2 RelOp Expr3 ;
EAnd. Expr1 ::= Expr2 "&&" Expr1 ;
EOr. Expr ::= Expr1 "||" Expr ;
coercions Expr 6 ;
separator Expr "," ;
-- operators -----------------------------------------------
Plus. AddOp ::= "+" ;
Minus. AddOp ::= "-" ;
Times. MulOp ::= "*" ;
Div. MulOp ::= "/" ;
Mod. MulOp ::= "%" ;
LTH. RelOp ::= "<" ;
LE. RelOp ::= "<=" ;
GTH. RelOp ::= ">" ;
GE. RelOp ::= ">=" ;
EQU. RelOp ::= "==" ;
NE. RelOp ::= "!=" ;
-- comments ------------------------------------------------
comment "#" ;
comment "//" ;
comment "/*" "*/" ;
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include "Parser.h"
#include "Printer.h"
#include "Absyn.h"
#include "Compiler.h"
#include <fstream>
#include <iostream>
#include <filesystem>
std::filesystem::path binaryPath;
void usage() {
printf("usage: Call with one of the following argument combinations:\n");
printf("\t--help\t\tDisplay this help message.\n");
printf("\t(no arguments) Parse stdin silently.\n");
printf("\t(files)\t\tParse content of files silently.\n");
printf("\t-v (files)\tVerbose mode. Parse content of files verbosely.\n");
}
int main(int argc, char ** argv)
{
FILE *input;
int quiet = 0;
char *filename = NULL;
if (argc > 1) {
if (strcmp(argv[1], "-v") == 0) {
if (argc > 2) {
filename = argv[2];
} else {
input = stdin;
}
} else {
quiet = 1;
filename = argv[1];
}
}
if (filename) {
input = fopen(filename, "r");
if (!input) {
usage();
exit(1);
}
} else input = stdin;
/* The default entry point is used. For other options see Parser.H */
Program *parse_tree = pProgram(input);
if (!parse_tree) {
fprintf(stderr, "Parser error, invalid syntax!");
return 1;
}
if (!quiet) {
fprintf(stderr, "\n[Abstract Syntax]\n");
ShowAbsyn s;
fprintf(stderr, "%s\n\n", s.show(parse_tree));
fprintf(stderr, "[Linearized Tree]\n");
PrintAbsyn p;
fprintf(stderr, "%s\n\n", p.print(parse_tree));
}
binaryPath = argv[0];
std::filesystem::path file(filename ? filename : "Instant");
Compiler c(file);
std::string out = c.compile(parse_tree);
if (!filename) {
printf("%s\n", out.data());
return 0;
}
file.replace_extension(Compiler::extension);
std::ofstream ll(file);
if (!ll) {
fprintf(stderr, "Unable to open %s\n", file.c_str());
return 2;
}
ll << out;
ll.close();
Compiler::externalCommand(file);
}
This diff is collapsed.
CC=g++
CCFLAGS=-g -W -Wall
FLEX=flex
FLEX_OPTS=-PGrammar
BISON=bison
BISON_OPTS=-t -pGrammar
OBJS=Absyn.o Lexer.o Parser.o Printer.o
.PHONY : clean distclean
all : TestGrammar
clean :
rm -f *.o TestGrammar Grammar.aux Grammar.log Grammar.pdf Grammar.dvi Grammar.ps Grammar
distclean : clean
rm -f Absyn.cpp Absyn.h Test.cpp Parser.cpp Parser.h Lexer.cpp Skeleton.cpp Skeleton.h Printer.cpp Printer.h Makefile Grammar.l Grammar.y Grammar.tex
TestGrammar : ${OBJS} Test.o
@echo "Linking TestGrammar..."
${CC} ${CCFLAGS} ${OBJS} Test.o -o TestGrammar
Absyn.o : Absyn.cpp Absyn.h
${CC} ${CCFLAGS} -c Absyn.cpp
Lexer.cpp : Grammar.l
${FLEX} -oLexer.cpp Grammar.l
Parser.cpp : Grammar.y
${BISON} Grammar.y -o Parser.cpp
Lexer.o : Lexer.cpp Parser.h
${CC} ${CCFLAGS} -c Lexer.cpp
Parser.o : Parser.cpp Absyn.h
${CC} ${CCFLAGS} -c Parser.cpp
Printer.o : Printer.cpp Printer.h Absyn.h
${CC} ${CCFLAGS} -c Printer.cpp
Skeleton.o : Skeleton.cpp Skeleton.h Absyn.h
${CC} ${CCFLAGS} -Wno-unused-parameter -c Skeleton.cpp
Test.o : Test.cpp Parser.h Printer.h Absyn.h
${CC} ${CCFLAGS} -c Test.cpp
This diff is collapsed.
#ifndef PARSER_HEADER_FILE
#define PARSER_HEADER_FILE
#include<vector>
#include<string>
class Program;
class TopDef;
class ListTopDef;
class FunDef;
class Arg;
class ListArg;
class ClassDef;
class ClassBlock;
class ClassBlockDef;
class ListClassBlockDef;
class Block;
class ListStmt;
class Stmt;
class Item;
class ListItem;
class Type;
class ListType;
class Expr;
class ListExpr;
class AddOp;
class MulOp;
class RelOp;
typedef union
{
int int_;
char char_;
double double_;
char* string_;
Program* program_;
TopDef* topdef_;
ListTopDef* listtopdef_;
FunDef* fundef_;
Arg* arg_;
ListArg* listarg_;
ClassDef* classdef_;
ClassBlock* classblock_;
ClassBlockDef* classblockdef_;
ListClassBlockDef* listclassblockdef_;
Block* block_;
ListStmt* liststmt_;
Stmt* stmt_;
Item* item_;
ListItem* listitem_;
Type* type_;
ListType* listtype_;
Expr* expr_;
ListExpr* listexpr_;
AddOp* addop_;
MulOp* mulop_;
RelOp* relop_;
} YYSTYPE;
Program* pProgram(FILE *inp);
Program* pProgram(const char *str);
#define _ERROR_ 258
#define _SYMB_0 259
#define _SYMB_1 260
#define _SYMB_2 261
#define _SYMB_3 262
#define _SYMB_4 263
#define _SYMB_5 264
#define _SYMB_6 265
#define _SYMB_7 266
#define _SYMB_8 267
#define _SYMB_9 268
#define _SYMB_10 269
#define _SYMB_11 270
#define _SYMB_12 271
#define _SYMB_13 272
#define _SYMB_14 273
#define _SYMB_15 274
#define _SYMB_16 275
#define _SYMB_17 276
#define _SYMB_18 277
#define _SYMB_19 278
#define _SYMB_20 279
#define _SYMB_21 280
#define _SYMB_22 281
#define _SYMB_23 282
#define _SYMB_24 283
#define _SYMB_25 284
#define _SYMB_26 285
#define _SYMB_27 286
#define _SYMB_28 287
#define _SYMB_29 288
#define _SYMB_30 289
#define _SYMB_31 290
#define _SYMB_32 291
#define _SYMB_33 292
#define _SYMB_34 293
#define _SYMB_35 294
#define _SYMB_36 295
#define _SYMB_37 296
#define _SYMB_38 297
#define _SYMB_39 298
#define _SYMB_40 299
#define _SYMB_41 300
#define _SYMB_42 301
#define _STRING_ 302
#define _INTEGER_ 303
extern YYSTYPE yylval;
#endif
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#ifndef SKELETON_HEADER
#define SKELETON_HEADER
/* You might want to change the above name. */
#include "Absyn.h"
class Skeleton : public Visitor
{
public:
void visitProgram(Program *p);
void visitTopDef(TopDef *p);
void visitFunDef(FunDef *p);
void visitArg(Arg *p);
void visitClassDef(ClassDef *p);
void visitClassBlock(ClassBlock *p);
void visitClassBlockDef(ClassBlockDef *p);
void visitBlock(Block *p);
void visitStmt(Stmt *p);
void visitItem(Item *p);
void visitType(Type *p);
void visitExpr(Expr *p);
void visitAddOp(AddOp *p);
void visitMulOp(MulOp *p);
void visitRelOp(RelOp *p);
void visitProg(Prog *p);
void visitFnDef(FnDef *p);
void visitClDef(ClDef *p);
void visitFuncDef(FuncDef *p);
void visitAr(Ar *p);
void visitClassDefN(ClassDefN *p);
void visitClassDefE(ClassDefE *p);
void visitClassBl(ClassBl *p);
void visitClassMthd(ClassMthd *p);
void visitClassFld(ClassFld *p);
void visitBlk(Blk *p);
void visitEmpty(Empty *p);
void visitBStmt(BStmt *p);
void visitDecl(Decl *p);
void visitNoInit(NoInit *p);
void visitInit(Init *p);
void visitAss(Ass *p);
void visitTableAss(TableAss *p);
void visitTableIncr(TableIncr *p);
void visitTableDecr(TableDecr *p);
void visitIncr(Incr *p);
void visitDecr(Decr *p);
void visitRet(Ret *p);
void visitVRet(VRet *p);
void visitCond(Cond *p);
void visitCondElse(CondElse *p);
void visitWhile(While *p);
void visitSExp(SExp *p);
void visitForEach(ForEach *p);
void visitInt(Int *p);
void visitStr(Str *p);
void visitBool(Bool *p);
void visitVoid(Void *p);
void visitArray(Array *p);
void visitClassT(ClassT *p);
void visitFun(Fun *p);
void visitEVar(EVar *p);
void visitELitInt(ELitInt *p);
void visitELitTrue(ELitTrue *p);
void visitELitFalse(ELitFalse *p);
void visitEApp(EApp *p);
void visitEString(EString *p);
void visitENewArray(ENewArray *p);
void visitENewClass(ENewClass *p);
void visitEClsMmbr(EClsMmbr *p);
void visitEClsMthd(EClsMthd *p);
void visitNull(Null *p);
void visitEIndexAcc(EIndexAcc *p);
void visitECast(ECast *p);
void visitNeg(Neg *p);
void visitNot(Not *p);
void visitEMul(EMul *p);
void visitEAdd(EAdd *p);
void visitERel(ERel *p);
void visitEAnd(EAnd *p);
void visitEOr(EOr *p);
void visitPlus(Plus *p);
void visitMinus(Minus *p);
void visitTimes(Times *p);
void visitDiv(Div *p);
void visitMod(Mod *p);
void visitLTH(LTH *p);
void visitLE(LE *p);
void visitGTH(GTH *p);
void visitGE(GE *p);
void visitEQU(EQU *p);
void visitNE(NE *p);
void visitListTopDef(ListTopDef *p);
void visitListArg(ListArg *p);
void visitListClassBlockDef(ListClassBlockDef *p);
void visitListStmt(ListStmt *p);
void visitListItem(ListItem *p);
void visitListType(ListType *p);
void visitListExpr(ListExpr *p);
void visitPIdent(PIdent *p);
void visitInteger(Integer x);
void visitChar(Char x);
void visitDouble(Double x);
void visitString(String x);
void visitIdent(Ident x);
};
#endif
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment