/* Propositional logic translator, infix-to-postfix 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() { char c = ch; if ( ch >= 'a' && ch <= 'z' ) ch = 'L'; switch( ch ) { case 'L': printf("%c", c); get_char(); break; case '-': get_char(); formula(); printf("N"); 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(); printf("K"); } } void expression() { term(); while ( ch == 'v' ) { get_char(); term(); printf("A"); } } void formula() { char c; expression(); while ( ch == '>' || ch == '=') { c = ch; get_char(); formula(); if ( c == '=' ) printf("E"); if ( c == '>' ) printf("C"); } } int main() { get_char(); formula(); if ( ch != '.') error ("Expected ."); return 0; }