/* Propositional logic parser Sten Morten Andersen, 2003 Grammar: formula ::= expression { ('>' | '=') formula } expression ::= term { 'v' term } term ::= factor { '&' factor } factor ::= 'a' | 'b' | 'c' | .. | 'z' | '-' formula | '(' formula ')' */ char ch; void formula(); void error(char *s) { printf("%s. Seen %c\n", s, ch); exit(1); } void get_char() { do ch = getchar(); while ( ch <= ' ' ); } void factor() { if ( ch >= 'a' && ch <= 'z' ) ch = 'L'; switch( ch ) { case 'L': get_char(); break; case '-': get_char(); formula(); break; case '(': get_char(); formula(); if ( ch != ')' ) error("Missing )"); get_char(); break; default: error("Expected a..z or (formula)"); } } void term() { factor(); while ( ch == '&' ) { get_char(); factor(); } } void expression() { term(); while ( ch == 'v' ) { get_char(); term(); } } void formula() { expression(); while ( ch == '>' || ch == '=') { get_char(); formula(); } } int main() { get_char(); formula(); if ( ch != '.') error ("Expected ."); return 0; }