#include #include char lookahead; int ok = 0; // I use 'ok' to check if there is any leftover input after // the parse is done. You may do this check differently. // void match(char t) // Function match() matches the current lookahead char. { // If the match is correct, update lookahead to the next if (lookahead==t) // char in the input stream. lookahead = getchar(); else { printf("Error in Match()\n"); exit(1); } } void L() { if (lookahead == '(') { match('('); // These lines match the production rule: L(); // L --> ( L ) L match(')'); // Each non-terminal in the rule is a L(); // function call in this code. } else if (lookahead == '[') { match('['); // These lines match the production rule: L(); // L --> [ L ] L match(']'); L(); } else return; // I'm using "return" to represent lambda, the empty string // portion of the production rule for L } int main() // Find the First set(s) for all the non-terminals in the grammar // In this grammar, the only non-terminal is L // The First set of L = { '(', '[' } // Match the First set by checking if lookahead is '(' or '[' // If yes, proceed with parsing the production rule. // Each non-terminal, L, is a function call. { lookahead = getchar(); // If lookahead == '(' or '[', call function L L(); if (lookahead != '\n') ok = 0; if (ok) printf("Accept\n"); else printf("Reject\n"); return 0; }