Skip to content

Commit

Permalink
Code from 4.4.1 added and run once
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Rijal authored and Anuraag Rijal committed Mar 23, 2017
1 parent 0d7a7c5 commit 53d52d7
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ void getNonBlank() {
getChar();
}

/*****************************************************/
/
*****************************************************/
/* lex - a simple lexical analyzer for arithmetic
expressions */
int lex() {
Expand Down Expand Up @@ -169,3 +170,69 @@ int lex() {
printf("Next token is: %d, Next lexeme is %s\n", nextToken, lexeme);
return nextToken;
} /* End of function lex */

/* expr
Parses strings in the language generated by the rule:
<expr> -> <term> {(+ | -) <term>}
*/
void expr() {
printf("Enter <expr>\n");
/* Parse the first term */
term();
/* As long as the next token is + or -, get
the next token and parse the next term */
while (nextToken == ADD_OP || nextToken == SUB_OP) {
lex();
term();
}
printf("Exit <expr>\n");
} /* End of function expr */

/* term
Parses strings in the language generated by the rule:
<term> -> <factor> {(* | /) <factor>)
*/
void term() {
printf("Enter <term>\n");
/* Parse the first factor */
factor();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == MULT_OP || nextToken == DIV_OP) {
lex();
factor();
}
printf("Exit <term>\n");
} /* End of function term */

/* factor
Parses strings in the language generated by the rule:
<factor> -> id | int_constant | ( <expr )
*/
void factor() {
printf("Enter <factor>\n");
/* Determine which RHS */
if (nextToken == IDENT || nextToken == INT_LIT)
/* Get the next token */
lex();
/* If the RHS is ( <expr>), call lex to pass over the
left parenthesis, call expr, and check for the right
parenthesis */
else {
if (nextToken == LEFT_PAREN) {
lex();
expr();
if (nextToken == RIGHT_PAREN)
lex();
else
error();
} /* End of if (nextToken == ... */

/* It was not an id, an integer literal, or a left
parenthesis */
else
error();
} /* End of else */

printf("Exit <factor>\n");;
} /* End of function factor */

0 comments on commit 53d52d7

Please sign in to comment.