الكاتب
سبحان الله وبحمده
using System; using System.Collections.Generic; using System.Text; namespace Postfix_Parser { classToken { publicstaticint code; publicstaticstring attribute = ""; } classParser { publicconstint id = 256; publicconstint num = 257; publicstaticstring input = ""; publicstaticstring p = ""; staticint x = 0; publicToken NextTerminal() { char c; Token token = newToken(); c = input[x]; if ((x + 1) < input.Length) { x++; } if (char.IsLetter(c) == true) { Token.attribute = c.ToString(); Token.code = id; } elseif (char.IsDigit(c) == true) { Token.attribute = c.ToString(); Token.code = num; } else { Token.code = (int)c; } return token; } publicvoid output() { p = ""; expr(); Console.WriteLine("The Postfix is : "); Console.WriteLine(p); Console.ReadLine(); } publicvoid expr() { int lookahead = Token.code; if (lookahead == id || lookahead == num || lookahead == (int)'-' || lookahead == (int)'(') { term(); edash(); } else { Console.WriteLine("syntx error"); } } publicvoid edash() { int lookahead = Token.code; switch (lookahead) { case (int)'+': match((int)'+'); term(); p += "+"; edash(); break; case (int)'-': match((int)'-'); term(); p += "-"; edash(); break; } } publicvoid term() { int lookahead = Token.code; if (lookahead == id || lookahead == num || lookahead == (int)'-' || lookahead == (int)')') { factor(); tdash(); } else { Console.WriteLine("syntx error"); } } publicvoid tdash() { int lookahead = Token.code; switch (lookahead) { case (int)'*': match((int)'*'); factor(); p += "*"; tdash(); break; case (int)'/': match((int)'/'); factor(); p += "/"; tdash(); break; } } publicvoid factor() { int lookahead = Token.code; string lex; switch (lookahead) { case id: lex = Token.attribute; match(id); p += lex; break; case num: match(num); p += Token.attribute; break; case'(': match((int)'('); expr(); match((int)')'); break; case'-': match((int)'-'); factor(); p += "-"; break; default: Console.WriteLine("syntx error"); break; } } publicvoid match(int t) { Parser parse = newParser(); if (Token.code == t) { parse.NextTerminal(); } else { Console.WriteLine("syntx error"); } } staticvoid Main(string[] args) { Parser parse = newParser(); Console.WriteLine("Please Enter the Infix Translation :"); input = Console.ReadLine(); Console.WriteLine(); parse.NextTerminal(); parse.output(); } } }