private void checkUnaryMinus(LexemaSequence input, Lexema lexema) { if (lexema.symbol == Expression.MINUS) { bool must_change = false; if (input.GetPosition() > 1) { char symbol = input.getLexema(input.GetPosition() - 2).symbol; if ((symbol == Expression.ASSIGNMENT) || (symbol == Expression.OPEN_BRACE) || (symbol == Expression.PLUS) || (symbol == Expression.MULTUPLICATION) || (symbol == Expression.BITWISE_OR) || (symbol == Expression.DIVISION) || (symbol == Expression.MODUL) || (symbol == Expression.BITWISE_AND) || (symbol == Expression.BITWISE_INVERSION) || (symbol == Expression.BOOLEAN_INVERSION)) { must_change = true; } } else { must_change = true; } if (must_change) { lexema.symbol = Expression.UNARY_MINUS; } } }
// парсваме в конструктора public Parser(String file_name) { if (File.Exists(file_name)) { StreamReader file = null; try { String line = null; int line_number = 0; file = new StreamReader(file_name); lexSeq = new LexemaSequence(); while ((line = file.ReadLine()) != null) { line_number++; ParseString(line, line_number); } } finally { if (file != null) { file.Close(); } } } else { Console.WriteLine(file_name + " not found."); } }
private bool changeSymbol(LexemaSequence input, Lexema lexema, char search_symbol, char replace_symbol) { if (lexema.symbol == search_symbol) { if (input.GetPosition() > 1) { if (input.getLexema(input.GetPosition() - 2).symbol == Expression.IDENTIFIER) { lexema.symbol = replace_symbol; return(true); } } if (input.GetPosition() < input.size()) { if (input.peekLexema().symbol == Expression.IDENTIFIER) { //това е префиксна операция, не се прави заместване return(true); } else { Console.WriteLine(search_symbol + " can be applyed only to identifier."); return(false); } } Console.WriteLine("Error 1002."); return(false); } else { return(true); } }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("usage: Translator.exe <input file>"); return; } String inputFileName = args[0]; String productionsFileName = "productions.txt"; Parser parser = new Parser(inputFileName); LexemaSequence input = parser.getSequence(); SyntaxAnalyser analyser = new SyntaxAnalyser(input, productionsFileName); if (analyser.Process() == false) { Console.WriteLine("Input sequence syntax is incorrect."); return; } // TranslationUnit tr_unit = new TranslationUnit(input); StreamWriter sw = new StreamWriter(new FileStream(inputFileName.Split(new char[] { '.' })[0] + ".asm", FileMode.OpenOrCreate, FileAccess.Write)); sw.Write(tr_unit.GetAsmCode()); sw.Close(); }
//открива и заменя постфиксни ++ и -- с други символи private bool incAndDecSymbolDeal(LexemaSequence input, Lexema lexema) { bool ret = true; ret = changeSymbol(input, lexema, Expression.PREFIX_INCREMENT, Expression.POSTFIX_INCREMENT); if (ret == true) { ret = changeSymbol(input, lexema, Expression.PREFIX_DECREMENT, Expression.POSTFIX_DECREMENT); } return(ret); }
private static Hashtable includedProcs = new Hashtable(); //<String, String> public TranslationUnit(LexemaSequence input_seq) { input = input_seq; loadExpressions(); asmHeader = "STACK_SEG SEGMENT STACK" + newLine + " DW 512 DUP(?)"+ newLine + "STACK_SEG ENDS" + newLine + newLine + "CODESC SEGMENT 'CODE'" + newLine + " ASSUME CS:CODESC, SS:STACK_SEG"+ newLine + newLine + "BEGIN: .386" + newLine; returnDOS = "MOV AH, 4CH" + newLine + "INT 21H" + newLine; asmFooter = "CODESC ENDS" + newLine + " END BEGIN" + newLine; parseStatements(); }
public SyntaxAnalyser(LexemaSequence inputSequence, String filName) { input = inputSequence; productionsFileName = filName; }