Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
added first step of Mini Java tutorial example
Browse files Browse the repository at this point in the history
  • Loading branch information
christophevg committed Feb 21, 2017
1 parent 1e84e32 commit 14d647a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
Binary file added assets/mini-java-1.bnf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example/mini-java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mini-java.cs
*.exe
*.mdb

38 changes: 38 additions & 0 deletions example/mini-java/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TARGET ?= mini-java
PARSER ?= $(TARGET).cs
GRAMMAR ?= $(TARGET).bnf
GENERATOR ?= ../../generator
EXE ?= parse-$(TARGET).exe
INPUT ?= example.$(TARGET)

-include ../../generator/Makefile.support

SRCS = $(GENERATOR)/parsable.cs $(GENERATOR)/dump-ast.cs $(PARSER)
TEST_SRCS = $(SRCS) test_*.cs

all: parse

test: test.dll
@echo "*** executing unit tests"
@$(NUNIT) $<

test.dll: $(TEST_SRCS)
@echo "*** building unit tests"
@$(CC) $(CFLAGS) -t:library -r:nunit.framework.dll -out:$@ $^

parse: $(EXE)
@echo "*** running $<"
@$(RUN) $< $(INPUT) | $(FORMATTER)

$(EXE): $(GRAMMAR) $(SRCS)
@echo "*** building $@ from $(filter-out $<,$^)"
@$(CC) $(CFLAGS) -out:$@ $(filter-out $<,$^)

$(PARSER): $(GRAMMAR) $(HPG)
@echo "*** generating a $(TARGET) parser from $<"
@$(RUN) $(HPG) $< | $(ASTYLE) > $@

clean:
@rm -rf $(BUILD) TestResult.xml $(PARSER) test.dll $(EXE) *.dot *.mdb

-include Makefile.local
16 changes: 16 additions & 0 deletions example/mini-java/example.mini-java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Factorial {
public static void main(String[] a){
System.out.println(new Fac().ComputeFac(10));
}
}

class Fac {
public int ComputeFac(int num) {
int num_aux ;
if (num < 1)
num_aux = 1 ;
else
num_aux = num * (this.ComputeFac(num-1)) ;
return num_aux ;
}
}
57 changes: 57 additions & 0 deletions example/mini-java/mini-java.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Program ::= MainClass { ClassDeclaration } ;

MainClass ::= "class" Identifier "{"
"public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{"
Statement
"}"
"}" ;

ClassDeclaration ::= "class" Identifier [ "extends" Identifier ] "{"
{ VarDeclaration }
{ MethodDeclaration }
"}" ;

VarDeclaration ::= Type Identifier ";" ;

MethodDeclaration ::= "public" Type Identifier "(" [ Type Identifier { "," Type Identifier } ] ")" "{"
{ VarDeclaration }
{ Statement }
"return" Expression ";"
"}" ;

Type ::= ( "int" "[" "]" )
| "boolean"
| "int"
| Identifier
;

Statement ::= ( "{" { Statement } "}" )
| ( "if" "(" Expression ")" Statement "else" Statement )
| ( "while" "(" Expression ")" Statement )
| ( "System.out.println" "(" Expression ")" ";" )
| ( Identifier "=" Expression ";" )
| ( Identifier "[" Expression "]" "=" Expression ";" )
;

Expression ::= AndExpression ;
AndExpression ::= LessThanExpression { "&&" LessThanExpression } ;
LessThanExpression ::= AdditiveExpression [ "<" AdditiveExpression ] ;
AdditiveExpression ::= TimesExpression { [ "+" | "-" ] TimesExpression } ;
TimesExpression ::= PrefixExpression { "*" PrefixExpression } ;
PrefixExpression ::= NotExpression | PostfixExpression;
NotExpression ::= "!" { "!" } PostfixExpression ;
PostfixExpression ::= PrimaryExpression { ( "[" Expression "]" )
| ( "." Identifier "(" [ Expression { "," Expression } ] ")" )
| ( "." "length" ) } ;
PrimaryExpression ::= Integer
| "true"
| "false"
| "this"
| ( "(" Expression ")" )
| ( "new" "int" "[" Expression "]" )
| ( "new" Identifier "(" ")" )
| Identifier
;

Identifier ::= /([A-Za-z_][A-Za-z0-9-_]*)/ ;
Integer ::= /(-?[1-9][0-9]*)/ ;
37 changes: 37 additions & 0 deletions example/mini-java/mini-java.original.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Program ::= MainClass ( ClassDeclaration )* <EOF>

MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" Statement "}" "}"

ClassDeclaration ::= "class" Identifier ( "extends" Identifier )? "{" ( VarDeclaration )* ( MethodDeclaration )* "}"

VarDeclaration ::= Type Identifier ";"

MethodDeclaration ::= "public" Type Identifier "(" ( Type Identifier ( "," Type Identifier )* )? ")" "{" ( VarDeclaration )* ( Statement )* "return" Expression ";" "}"

Type ::= "int" "[" "]"
| "boolean"
| "int"
| Identifier

Statement ::= "{" ( Statement )* "}"
| "if" "(" Expression ")" Statement "else" Statement
| "while" "(" Expression ")" Statement
| "System.out.println" "(" Expression ")" ";"
| Identifier "=" Expression ";"
| Identifier "[" Expression "]" "=" Expression ";"

Expression ::= Expression ( "&&" | "<" | "+" | "-" | "*" ) Expression
| Expression "[" Expression "]"
| Expression "." "length"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")"
| <INTEGER_LITERAL>
| "true"
| "false"
| Identifier
| "this"
| "new" "int" "[" Expression "]"
| "new" Identifier "(" ")"
| "!" Expression
| "(" Expression ")"

Identifier ::= <IDENTIFIER>

0 comments on commit 14d647a

Please sign in to comment.