This repository was archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added first step of Mini Java tutorial example
- Loading branch information
1 parent
1e84e32
commit 14d647a
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mini-java.cs | ||
*.exe | ||
*.mdb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]*)/ ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |