Skip to content

Commit

Permalink
Part 12: Fix declarations grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
holyketzer committed Feb 6, 2021
1 parent 69fbdea commit b9111d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
22 changes: 17 additions & 5 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
# | declarations compound_statement

# declarations:
# | VAR (variable_declaration SEMI)+ (procedure_declaration)*
# | (procedure_declaration)*
# | empty
# | (declaration)*

# declaration:
# | VAR (variable_declaration SEMI)+
# | (procedure_declaration)+

# procedure_declaration:
# | PROCEDURE ID SEMI block SEMI
Expand Down Expand Up @@ -385,14 +387,24 @@ def block(self):

def declarations(self):
res = []

declaration = self.declaration()
while declaration:
res += declaration
declaration = self.declaration()

return res

def declaration(self):
res = []

if self.current_token.type == VAR:
self.eat(VAR)

while self.current_token.type == ID:
res += self.variable_declaration()
self.eat(SEMI)

while self.current_token.type == PROCEDURE:
elif self.current_token.type == PROCEDURE:
res.append(self.procedure_declaration())

return res
Expand Down
15 changes: 15 additions & 0 deletions test.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
a, b : INTEGER;
y : REAL;

PROCEDURE p1;
var x : INTEGER;
begin
x := 1;
end;

VAR
nnn : INTEGER;

PROCEDURE p2;
var x2 : INTEGER;
begin
x2 := 1;
end;

BEGIN {Part11}
number := 2;
a := number ;
Expand Down

0 comments on commit b9111d7

Please sign in to comment.