#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);
void T(void);
void S(void);
void F(void);
void error(int);
void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;
void main(void) {
scan();
E();
if (next != '$') error(1);
else printf("Success\n");
}
void E(void) {
enter('E');
T();
while (next == '+' || next == '-') {
scan();
T();
}
leave('E');
}
void T(void) {
enter('T');
S();
while (next == '*' || next == '/') {
scan();
S();
}
leave('T');
}
void S(void) {
enter('S');
F();
if (next == '^') {
scan();
S();
}
leave('S');
}
void F(void) {
enter('F');
if (isalpha(next)) {
scan();
}
else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(2);
}
else {
error(3);
}
leave('F');
}
void scan(void) {
while (isspace(next = getchar()))
;
}
void error(int n) {
printf("\n*** ERROR: %i\n", n);
exit(1);
}
void enter(char name) {
spaces(level++);
printf("+-%c: Enter, \t", name);
printf("Next == %c\n", next);
}
void leave(char name) {
{
spaces(--level);
printf("+-%c: Leave, \t", name);
printf("Next == %c\n", next);
}
void spaces(int local_level) {
while (local_level-- > 0)
printf("| ");
}
| /* Arith0.java: debug parser
* grammar:
* P ---> E '$'
* E ---> T {('+'|'-') T}
* T ---> S {('*'|'/') S}
* S ---> F '^' S | F
* F ---> char | '(' E ')'
*/
class Arith0 {
private int level = 0;
private GetChar getChar = new GetChar();
private char next;
private void P() {
scan();
E();
if (next != '$') error(1);
else System.out.println("Success");
}
private void E() {
enter('E');
T();
while (next == '+' || next == '-') {
scan();
T();
}
leave('E');
}
private void T() {
enter('T');
S();
while (next == '*' || next == '/') {
scan();
S();
}
leave('T');
}
private void S() {
enter('S');
F();
if (next == '^') {
scan();
S();
}
leave('S');
}
private void F() {
enter('F');
if (Character.isLetter(next)) {
scan();
}
else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(2);
}
else {
error(3);
}
leave('F');
}
private void scan() {
while (Character.isWhitespace(next =
getChar.getNextChar()))
;
}
private void error(int n) {
System.out.println("*** ERROR: " + n);
System.exit(1);
}
private void enter(char name) {
spaces(level++);
System.out.print("+-" + name +
": Enter, \t");
System.out.println("Next == " + next);
}
private void leave(char name) {
spaces(--level);
System.out.print("+-" + name +
": Leave, \t");
System.out.println("Next == " + next);
}
private void spaces(int local_level) {
while (local_level-- > 0)
System.out.print("| ");
}
public static void main(String[] args) {
Arith0 arith0 = new Arith0();
arith0.P();
}
}
|
% arith0
a + b + c $
+-E: Enter, Next == a
| +-T: Enter, Next == a
| | +-S: Enter, Next == a
| | | +-F: Enter, Next == a
| | | +-F: Leave, Next == +
| | +-S: Leave, Next == +
| +-T: Leave, Next == +
| +-T: Enter, Next == b
| | +-S: Enter, Next == b
| | | +-F: Enter, Next == b
| | | +-F: Leave, Next == +
| | +-S: Leave, Next == +
| +-T: Leave, Next == +
| +-T: Enter, Next == c
| | +-S: Enter, Next == c
| | | +-F: Enter, Next == c
| | | +-F: Leave, Next == $
| | +-S: Leave, Next == $
| +-T: Leave, Next == $
+-E: Leave, Next == $
Success
% arith0
a ^ b ^ c $
+-E: Enter, Next == a
| +-T: Enter, Next == a
| | +-S: Enter, Next == a
| | | +-F: Enter, Next == a
| | | +-F: Leave, Next == ^
| | | +-S: Enter, Next == b
| | | | +-F: Enter, Next == b
| | | | +-F: Leave, Next == ^
| | | | +-S: Enter, Next == c
| | | | | +-F: Enter, Next == c
| | | | | +-F: Leave, Next == $
| | | | +-S: Leave, Next == $
| | | +-S: Leave, Next == $
| | +-S: Leave, Next == $
| +-T: Leave, Next == $
+-E: Leave, Next == $
Success |
% arith0
( a * b $
+-E: Enter, Next == (
| +-T: Enter, Next == (
| | +-S: Enter, Next == (
| | | +-F: Enter, Next == (
| | | | +-E: Enter, Next == a
| | | | | +-T: Enter, Next == a
| | | | | | +-S: Enter, Next == a
| | | | | | | +-F: Enter, Next == a
| | | | | | | +-F: Leave, Next == *
| | | | | | +-S: Leave, Next == *
| | | | | | +-S: Enter, Next == b
| | | | | | | +-F: Enter, Next == b
| | | | | | | +-F: Leave, Next == $
| | | | | | +-S: Leave, Next == $
| | | | | +-T: Leave, Next == $
| | | | +-E: Leave, Next == $
*** ERROR: 2
% arith0
(a + b) * c $
+-E: Enter, Next == (
| +-T: Enter, Next == (
| | +-S: Enter, Next == (
| | | +-F: Enter, Next == (
| | | | +-E: Enter, Next == a
| | | | | +-T: Enter, Next == a
| | | | | | +-S: Enter, Next == a
| | | | | | | +-F: Enter, Next == a
| | | | | | | +-F: Leave, Next == +
| | | | | | +-S: Leave, Next == +
| | | | | +-T: Leave, Next == +
| | | | | +-T: Enter, Next == b
| | | | | | +-S: Enter, Next == b
| | | | | | | +-F: Enter, Next == b
| | | | | | | +-F: Leave, Next == )
| | | | | | +-S: Leave, Next == )
| | | | | +-T: Leave, Next == )
| | | | +-E: Leave, Next == )
| | | +-F: Leave, Next == *
| | +-S: Leave, Next == *
| | +-S: Enter, Next == c
| | | +-F: Enter, Next == c
| | | +-F: Leave, Next == $
| | +-S: Leave, Next == $
| +-T: Leave, Next == $
+-E: Leave, Next == $
Success
|