Skip to content

Commit

Permalink
fix end of stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedelsa3eed committed Dec 25, 2023
2 parents 3b88930 + fb1d6bf commit cf09ef7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 66 deletions.
22 changes: 5 additions & 17 deletions CFG.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# METHOD_BODY = STATEMENT_LIST
# STATEMENT_LIST = STATEMENT | STATEMENT_LIST STATEMENT
# STATEMENT = DECLARATION
| IF
| WHILE
| ASSIGNMENT
# DECLARATION = PRIMITIVE_TYPE 'id' ';'
# PRIMITIVE_TYPE = 'int' | 'float'
# IF = 'if' '(' EXPRESSION ')' '{' STATEMENT '}' 'else' '{' STATEMENT '}'
# WHILE = 'while' '(' EXPRESSION ')' '{' STATEMENT '}'
# ASSIGNMENT = 'id' 'assign' EXPRESSION ';'
# EXPRESSION = SIMPLE_EXPRESSION
| SIMPLE_EXPRESSION 'relop' SIMPLE_EXPRESSION
# SIMPLE_EXPRESSION = TERM | SIGN TERM | SIMPLE_EXPRESSION 'addop' TERM
# TERM = FACTOR | TERM 'mulop' FACTOR
# FACTOR = 'id' | 'num' | '(' EXPRESSION ')'
# SIGN = '+' | '-'
# E = T E1
# E1 = 'add' T E1 | Epsilon
# T = F T1
# T1 = 'mul' F T1 | Epsilon
# F = '(' E ')' | id
35 changes: 1 addition & 34 deletions program.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
!!!!!
@@@@@
1.5E-6
int sum , count , pass , mnt; whi!=
le (pass !1.5E !
10 )
{
pass = pass + 1;
}
boolean float int intx!
!
whi!
@int sum , count , pass , mnt; whi! 1.5E-6
do!
1.5E-6
@int sum , count , pass , mnt; whi!
le (pass ! !
10 )
}
boolean float int intx!
!
whi!

do!
dou!
whi!le
do!
1.5E while
1.5E 123
1.5E
1.5E@
do!
@
1.5E
* + id ) + ( id *
16 changes: 4 additions & 12 deletions rules.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
letter = a- z | A-Z
digit = 0 - 9
id: letter (letter | digit)*
digits = digit+
{boolean int float}
num: digit+ | digit+ . digits (\L | E digits)
relop: \=\= | !\= | > | >\= | < | <\=
assign: \=
{while if else}
[; , \( \) { }]
addop: \+ | \-
mulop: \* | /
id: id
mul: \*
add: \+
[\( \)]
12 changes: 9 additions & 3 deletions src/Parser/PredictiveTopDownParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool PredictiveTopDownParser::handleMatchOrError(const StackItem &top, Token *&c
if (top.token == *(curr_token->getKey())) {
handleMatch(top, curr_token);
}
else if (top.token == EPSILON) {
else if (top.token == "$") {
handleEndOfStack(curr_token);
}
else {
Expand All @@ -75,11 +75,17 @@ void PredictiveTopDownParser::handleMatch(const StackItem &top, Token *&curr_tok
}

void PredictiveTopDownParser::handleEndOfStack(Token *&curr_token) {
parsingFile << "Error: ``" << *(curr_token->getKey()) << "`` discarded |\n";
curr_token = lex.getNextToken();
while (*(curr_token->getKey()) != "$") {
parsingFile << "Error: ``" << *(curr_token->getKey()) << "`` discarded |";
parsingFile << "| $ | " << *(curr_token->getKey())
<< " | Error: ``" << *(curr_token->getKey()) << "`` discarded |";

std::cout << "Error: " << *(curr_token->getKey()) << " discarded" << std::endl;
std::cerr << "Error: " << *(curr_token->getKey()) << " discarded" << std::endl;
curr_token = lex.getNextToken();
if (*(curr_token->getKey()) != "$") {
parsingFile << "\n";
}
}
}

Expand Down

0 comments on commit cf09ef7

Please sign in to comment.