/// <summary> /// Gets token and identifier tables from lexical analyzer. /// </summary> /// <param name="token">Token table.</param> /// <param name="ident">Identifiers table.</param> public SyntaxAnalyzer(TokenTable token, IdentifiersTable ident, LexicalAnalyzer lAnalyzer) { this.token = token; this.ident = ident; this.lAnalyzer = lAnalyzer; count = this.token.tokensCount(); }
static void Main(string[] args) { LexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer(); IdentifiersTable ident; TokenTable table = lexicalAnalyzer.analyze("Input5.txt", out ident); lexicalAnalyzer.printResults("Tokens.txt"); SyntaxAnalyzer syntaxAnalyzer = new SyntaxAnalyzer(table, ident, lexicalAnalyzer); syntaxAnalyzer.analyze(); SyntaxTree tree = syntaxAnalyzer.getTree(); tree.printItselfToTheFile("Tree.txt"); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(tree); semanticAnalyzer.analyze(); Generator generator = new Generator(); generator.generateExe(tree); Console.ReadLine(); }
public static void Main(string[] args) { //some text to lex StringBuilder sb = new StringBuilder(); AppendLine("for in while //this is a comment", sb); AppendLine(" ", sb); AppendLine("/*", sb); AppendLine("multi", sb); AppendLine("line", sb); AppendLine("comment", sb); AppendLine("*/", sb); AppendLine("fofds \"afdsafd\" whdsafdsa", sb); //let's turn the text into a list of char/int pairs IList <CharIntPair> cips = new List <CharIntPair> (); int i = 0; foreach (char c in sb.ToString()) { cips.Add(new CharIntPair(c, i)); ++i; } //to lex, we need a list of token types IList <TokenType <CharIntPairLexerAdapter> > types = new List <TokenType <CharIntPairLexerAdapter> >(); //add a token type for a stretch of whitespace types.Add(new TokenType <CharIntPairLexerAdapter>( WhitespaceRecognizer(), delegate(IList <CharIntPairLexerAdapter> ciplas) { //add up numbers associated with objects int sum = 0; foreach (CharIntPairLexerAdapter cipla in ciplas) { sum += cipla.cip.i; } Console.WriteLine("Got " + ciplas.Count + " chars of whitespace with sum " + sum); } )); //add some keywords types.Add(new TokenType <CharIntPairLexerAdapter>( FSM.ForConstantStringPattern("for").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) { Console.WriteLine("Got for at index " + ciplas[0].cip.i); } )); types.Add(new TokenType <CharIntPairLexerAdapter>( FSM.ForConstantStringPattern("in").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) { Console.WriteLine("Got in at index " + ciplas[0].cip.i); } )); types.Add(new TokenType <CharIntPairLexerAdapter> ( FSM.ForConstantStringPattern("while").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) { Console.WriteLine("Got while at index " + ciplas [0].cip.i); } )); //add a token type for a single line comment types.Add(new TokenType <CharIntPairLexerAdapter>( QuotePatternRecognizer("//", System.Environment.NewLine), delegate(IList <CharIntPairLexerAdapter> ciplas) { StringBuilder sb2 = new StringBuilder(); foreach (CharIntPairLexerAdapter cipla in ciplas) { sb2.Append(cipla.cip.c); } Console.WriteLine("Got single line comment:"); Console.Write(sb2.ToString()); } )); //add a token type for a multiline comment types.Add(new TokenType <CharIntPairLexerAdapter>( QuotePatternRecognizer("/*", "*/"), delegate(IList <CharIntPairLexerAdapter> ciplas) { StringBuilder sb2 = new StringBuilder(); foreach (CharIntPairLexerAdapter cipla in ciplas) { sb2.Append(cipla.cip.c); } Console.WriteLine("Got multiline comment:"); Console.WriteLine(sb2.ToString()); } )); //add a token type for a string literal types.Add(new TokenType <CharIntPairLexerAdapter>( QuotePatternRecognizer("\"", "\""), delegate(IList <CharIntPairLexerAdapter> ciplas) { StringBuilder sb2 = new StringBuilder(); foreach (CharIntPairLexerAdapter cipla in ciplas) { sb2.Append(cipla.cip.c); } Console.WriteLine("Got string literal:"); Console.WriteLine(sb2.ToString()); } )); LexicalAnalyzer <CharIntPairLexerAdapter> lexer = new LexicalAnalyzer <CharIntPairLexerAdapter>( types, delegate(CharIntPairLexerAdapter cipla) { Console.WriteLine("Got unhandlable char " + cipla.cip.c + " with number " + cipla.cip.i); } ); foreach (CharIntPair cip in cips) { lexer.ProcessItem(new CharIntPairLexerAdapter(cip)); } lexer.ProcessEOF(); }