Syntax Finished

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

1
.gitignore vendored

@ -0,0 +1 @@
target/

@ -1,55 +1,100 @@
module Syntax
extend lang::std::Layout;
extend lang::std::Whitespace;
extend lang::std::Id;
extend lang::std::ASCII;
start syntax Progarm = '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 ";"
start syntax Program = 'program' Id ";" ConstDecl* VarDecl* SubProgDecl* 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 StatementList = Statement {";" Statement}*;
syntax Statement = Lhs ":=" Expr |
ProcedureCall |
CompoundStatement |
'skip' |
'if' Guard 'then' Statement 'else' Statement |
'while' Guard 'do' Statement
;
syntax Guard = Expr Relop Expr |
'not' Guard |
Guard 'or' Guard |
Guard 'and' GUard |
"(" Guard ")"
;
syntax LhsList = Lhs {"," Lhs}*;
syntax Lhs = Id ("[" ExprList "]")?;
syntax ProcedureCall = Id ("(" ExprList ")")? |
'readln'"(" LhsList ")" |
'writeln'"(" ExprList ")"
;
syntax ExprList = Expr {"," Expr}*;
syntax Expr = Id ("[" Expr (".." Expr)?"]")? |
Num |
Id "(" ExprList ")" |
Expr "+" Expr |
Expr "-" Expr |
Expr "*" Expr |
Expr "/" Expr |
Expr 'div' Expr |
Expr 'mod' Expr |
"-" Expr |
"(" Expr ")"
;
lexical Relop = "\<"|"\<="|"\>"|"\>="|"=="|"!=";
lexical Number = Digit* ("." Digit+)?;
syntax StatementList = Statement
| StatementList ";" Statement;
syntax Statement = Lhs ":=" Expr
| ProcedureCall
| CompoundStatement
| 'skip'
| 'if' Guard 'then' Statement 'else' Statement
| 'while' Guard 'do' Statement
;
syntax Guard = "(" Guard ")"
> Expr Relop Expr
> 'not' Guard
> left (
Guard 'or' Guard
| Guard 'and' Guard
)
;
syntax LhsList = Lhs ("," Lhs)*
;
syntax Lhs = Id ("[" Expr "]")?
;
syntax ProcedureCall = 'readln'"(" LhsList ")"
| 'writeln'"(" ExprList ")"
> Id \ 'writeln' \ 'readln' \ 'skip' ("(" ExprList ")")?
;
syntax ExprList = Expr ("," Expr)*
;
syntax Expr = "(" Expr ")"
| Id
| Id "[" Expr (".." Expr)?"]"
> Number
| Id "(" ExprList ")"
> "-" 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