Syntax Finished

main
Channa Dias Perera 10 months ago
parent f23a84e409
commit 89e60b4136

1
.gitignore vendored

@ -0,0 +1 @@
target/

@ -1,55 +1,100 @@
module Syntax module Syntax
extend lang::std::Layout; extend lang::std::Whitespace;
extend lang::std::Id; extend lang::std::Id;
extend lang::std::ASCII; extend lang::std::ASCII;
start syntax Progarm = 'program' Id ";" ConstDecl* VarDecl* SubProgDecl* CompoundStatement; start syntax Program = 'program' Id ";" ConstDecl* VarDecl* SubProgDecl* CompoundStatement "."
syntax ConstDecl = 'const' Id "=" Number;
syntax IdList = Id {"," Id}*;
syntax TypeSpec = BasicType | 'array' "[" Number ".." Number "]" 'of' BasicType;
syntax BasicType = 'integer' | 'real';
syntax SubProgDecl = 'function' Id Parameters ":" BasicType ";" VarDecl* CompoundStatement ";" |
'procedure' Id Parameters? ";" VarDecl* CompoundStatement ";"
; ;
syntax Parameters = "(" ParameterList ")";
syntax ParameterList = 'var'? IdList ":" TypeSpec {";" ParameterList}*; syntax ConstDecl = 'const' Id "=" Number ";"
;
syntax VarDecl = 'var' IdList ":" TypeSpec ";"
;
syntax IdList = Id ("," Id)*
;
syntax TypeSpec = BasicType | 'array' "[" Number ".." Number "]" 'of' BasicType
;
syntax BasicType = 'integer' | 'real'
;
syntax SubProgDecl = 'function' Id Parameters ":" BasicType ";" VarDecl* CompoundStatement ";"
| 'procedure' Id Parameters? ";" VarDecl* CompoundStatement ";"
;
syntax Parameters = "(" ParameterList ")"
;
syntax ParameterList = ParamList
| ParameterList ";" ParamList
;
syntax ParamList = 'var'? IdList ":" TypeSpec
;
syntax CompoundStatement = 'begin' StatementList? 'end'; syntax CompoundStatement = 'begin' StatementList? 'end';
syntax StatementList = Statement {";" Statement}*;
syntax Statement = Lhs ":=" Expr | syntax StatementList = Statement
ProcedureCall | | StatementList ";" Statement;
CompoundStatement | syntax Statement = Lhs ":=" Expr
'skip' | | ProcedureCall
'if' Guard 'then' Statement 'else' Statement | | CompoundStatement
'while' Guard 'do' Statement | 'skip'
; | 'if' Guard 'then' Statement 'else' Statement
| 'while' Guard 'do' Statement
syntax Guard = Expr Relop Expr | ;
'not' Guard |
Guard 'or' Guard | syntax Guard = "(" Guard ")"
Guard 'and' GUard | > Expr Relop Expr
"(" Guard ")" > 'not' Guard
; > left (
Guard 'or' Guard
syntax LhsList = Lhs {"," Lhs}*; | Guard 'and' Guard
syntax Lhs = Id ("[" ExprList "]")?; )
syntax ProcedureCall = Id ("(" ExprList ")")? | ;
'readln'"(" LhsList ")" |
'writeln'"(" ExprList ")" syntax LhsList = Lhs ("," Lhs)*
; ;
syntax ExprList = Expr {"," Expr}*; syntax Lhs = Id ("[" Expr "]")?
syntax Expr = Id ("[" Expr (".." Expr)?"]")? | ;
Num |
Id "(" ExprList ")" | syntax ProcedureCall = 'readln'"(" LhsList ")"
Expr "+" Expr | | 'writeln'"(" ExprList ")"
Expr "-" Expr | > Id \ 'writeln' \ 'readln' \ 'skip' ("(" ExprList ")")?
Expr "*" Expr | ;
Expr "/" Expr |
Expr 'div' Expr | syntax ExprList = Expr ("," Expr)*
Expr 'mod' Expr | ;
"-" Expr | syntax Expr = "(" Expr ")"
"(" Expr ")" | Id
; | Id "[" Expr (".." Expr)?"]"
> Number
lexical Relop = "\<"|"\<="|"\>"|"\>="|"=="|"!="; | Id "(" ExprList ")"
lexical Number = Digit* ("." Digit+)?; > "-" Expr
> left (
Expr "*" Expr
| Expr "/" Expr
| Expr 'div' Expr
| Expr 'mod' Expr
)
> left (
Expr "+" Expr
| Expr "-" Expr
)
;
lexical Relop = "\<"|"\<="|"\>"|"\>="|"="|"\<\>";
lexical Number = Digit+
| Digit* "." Digit+
;
lexical Comment = "{" ![}]* "}";
layout Standard
= WhitespaceOrComment* !>> [\u0009-\u000D \u0020 \u0085 \u00A0 \u1680 \u180E \u2000-\u200A \u2028 \u2029 \u202F \u205F \u3000 \ \t\n\r] !>> [{}];
lexical WhitespaceOrComment
= whitespace: Whitespace
| comment: Comment
;

@ -0,0 +1,27 @@
module Test
import ParseTree;
import Syntax;
import IO;
void main() {
// The input files
list[loc] files = getFilesIn(|project://minipas/test/input|);
for (loc file <- files) {
pt = runSyntaxTestOn(file);
}
}
/*
Parses the file
*/
start[Program] runSyntaxTestOn(loc file) {
println("Parsing: <file>");
pt = parse(#start[Program], file);
return pt;
}
list[loc] getFilesIn(loc dir) {
return [|project://minipas/test/input/<file>| | file <- listEntries(dir)];
}

@ -1,18 +0,0 @@
module MyTest
import ParseTree;
void main() {
[loc] files = getFilesIn(|project://test/input|);
for (loc file <- files) {
runTestOn(file);
}
}
void runTestOn(loc file) {
println(file);
}
void getFilesIn(loc dir) {
return |project://test/input/arr.pas|;
}

@ -0,0 +1,8 @@
program comm;
{
comment
}
begin
end.
Loading…
Cancel
Save