/* Bare.java: parser for Recitation 6
* grammar:
* M ---> L '$'
* L ---> S { S }
* S ---> A | P | C
* A ---> lower-case '=' E ';'
* P ---> '<' E ';'
* G ---> '>' lower-case ';'
* C ---> '<' upper-case ';'
* E ---> T {('+' | '-') T}
* T ---> U {('*' | '/' ) U}
* U ---> F
* F ---> '(' E ')' | lower-case | digit
*/
import java.util.Date;
class Bare {
private GetChar getChar = new GetChar();
private char next;
private void M() {
scan();
L();
if (next != '$') error(0);
else System.out.println("Success");
}
private void L() {
S();
while (next == '<' ||
Character.isLowerCase(next)) {
S();
}
}
private void S() {
if (Character.isLowerCase(next)) A();
else if (next == '<') { // output
scan();
if (Character.isUpperCase(next)) {
System.out.println("Output, using: "+
next);
scan();
if (next == ';') scan();
else error(13);
}
else { // now general expression
System.out.println("Output express");
E();
if (next == ';') scan();
else error(12);
}
}
else error(3);
}
private void A() {
if (Character.isLowerCase(next)) {
System.out.println("Assign: " +
next + "= stuff");
scan();
}
else error(9);
if (next == '=') scan();
else error(10);
E();
if (next == ';') scan();
else error(11);
}
private void E() {
T();
while (next == '+' || next == '-') {
System.out.println("Op: " + next);
scan();
T();
}
}
private void T() {
U();
while (next == '*' || next == '/' ) {
System.out.println("Op: " + next);
scan();
U();
}
}
private void U() {
F();
}
private void F() {
if (Character.isLowerCase(next)) {
System.out.println("Processing: "+next);
scan();
}
else if (Character.isDigit(next)) {
System.out.println("Processing: "+next);
scan();
}
else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(14);
}
else {
error(15);
}
}
private void scan() {
while (Character.isWhitespace(next =
getChar.getNextChar()))
;
}
private void error(int n) {
System.out.println("*** ERROR: " + n);
System.exit(1);
}
public static void main(String[] args) {
Bare bare = new Bare();
bare.M();
}
}
| #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void L(void);
void S(void);
void A(void);
void E(void);
void T(void);
void U(void);
void F(void);
void error(int);
void scan(void);
int main() {
scan();
L();
if (next != '$') error(1);
else printf("Success\n");
}
void L(void) {
S();
while(next == '<' || islower(next)){
S();
}
}
void S(void) {
if (islower(next)) A(); // assign
else if (next == '<') { // output
scan();
if (isupper(next)) { // capital
printf("Output, using: %c\n",
next);
scan();
if (next == ';') scan();
else error(13);
}
else { // now general expression
printf("Output expression\n");
E();
if (next == ';') scan();
else error(12);
}
}
else error(3);
}
void A(void) {
if (islower(next)) {
printf("Assign: %c = stuff\n",
next);
scan();
}
else error(9);
if (next == '=') scan();
else error(10);
E();
if (next == ';') scan();
else error(11);
}
void E(void) {
T();
while (next == '+' || next == '-') {
printf("Op: %c\n", next);
scan();
T();
}
}
void T(void) {
U();
while (next == '*' || next == '/') {
printf("Op: %c\n", next);
scan();
U();
}
}
void U(void) {
F();
}
void F(void) {
if (islower(next)) {
printf("Processing: %c\n", next);
scan();
}
else if (isdigit(next)) {
printf("Processing: %c\n", next);
scan();
}
else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(2);
}
else {
error(3);
}
}
void scan(void) {
while (isspace(next = getchar()))
;
}
void error(int n) {
printf("\n*** ERROR: %i\n", n);
exit(1);
}
|