void _Parser_OnReduce(LALRParser parser, ReduceEventArgs args) { Func <Parser, NonterminalToken, object> handler; if (Internal.GrammarRules.Handlers.TryGetValue((RuleConstants)args.Rule.Id, out handler)) { try { args.Token.UserObject = handler(this, args.Token); } catch (Exception ex) { Errors.Add(string.Format("{0}: {1}", args.Rule.ToString(), ex.Message)); args.Continue = false; } return; } //This may happen for things that are not implemented yet because they //"unbalance" the AST if (args.Token.Tokens.Length != 1) { Errors.Add(string.Format("{0}: Internal problem", args.Rule.ToString())); args.Continue = false; return; } args.Token.UserObject = args.Token.Tokens[0].UserObject; }
/// <summary> /// Event handler for the action of reading a token. /// </summary> /// <param name="parser">parser that is the source of this event</param> /// <param name="args">event arguments</param> private void TokenReadEvent(LALRParser parser, TokenReadEventArgs args) { AddViewItem("Token Read", args.Token.Location, args.Token.Symbol.ToString(), StringUtil.ShowEscapeChars(args.Token.Text), "", 0); }
private void AcceptEvent(LALRParser parser, AcceptEventArgs args) { //System.Console.WriteLine("AcceptEvent "); NonterminalToken T = (NonterminalToken)args.Token; /* * try * { * Configuration.Load(Configuration.DefaultConfigurationPath); * this.Context.module = new MemoryRegion(); * } * catch (System.Exception ex) * { * Configuration.CreateDefaultConfiguration(); * Configuration.Load(Configuration.DefaultConfigurationPath); * this.Context.module = new MemoryRegion(); * }*/ if (this.RunMode == Rule_Context.eRunMode.Enter) { //mContext.EnterCheckCodeInterface = this.EnterCheckCodeInterface; mContext.RunMode = Rule_Context.eRunMode.Enter; } else if (this.RunMode == Rule_Context.eRunMode.Analysis) { //mContext.AnalysisCheckCodeInterface = this.AnalysisCheckCodeInterface; mContext.RunMode = Rule_Context.eRunMode.Analysis; } this.ProgramStart = new Rule_Statements(mContext, T); this.ProgramStart.Execute(); }
private void OpenGrammar() { if (openGrammarDialog.ShowDialog() == DialogResult.OK) { settings.LastGrammar = openGrammarDialog.FileName; SaveSettings(); WriteLn("Reading file..."); long t1 = DateTime.Now.Ticks; try { CGTReader reader = new CGTReader(openGrammarDialog.FileName); parser = reader.CreateNewParser(); parser.OnTokenRead += new LALRParser.TokenReadHandler(TokenReadEvent); parser.OnShift += new LALRParser.ShiftHandler(ShiftEvent); parser.OnReduce += new LALRParser.ReduceHandler(ReduceEvent); parser.OnGoto += new LALRParser.GotoHandler(GotoEvent); parser.OnAccept += new LALRParser.AcceptHandler(AcceptEvent); parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); parser.OnCommentRead += new LALRParser.CommentReadHandler(CommentReadEvent); long t2 = DateTime.Now.Ticks; WriteLn(String.Format("Reading the Compiled Grammar Table File took {0}ms", (t2 - t1) / 10000)); } catch (Exception e) { WriteLn(e.Message); } } }
public void testCalithaEngine(string cgtFile, string sourceFile) { FileStream fs = new FileStream(cgtFile, FileMode.Open, FileAccess.Read); CGTReader reader = new CGTReader(fs); LALRParser parser = reader.CreateNewParser(); parser.StoreTokens = LALRParser.StoreTokensMode.Never; fs.Close(); FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(sourceStream); String source = sr.ReadToEnd(); parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); TimeSpan timeStart = new TimeSpan(DateTime.Now.Ticks); parser.Parse(source); /* * for (int i = 0; i < 1000; i++) * { * parser.Parse(source); * } */ TimeSpan timeEnd = new TimeSpan(DateTime.Now.Ticks); this.outputBox.AppendText("Took " + timeEnd.Subtract(timeStart).ToString() + "\n"); }
private void TokenErrorEvent(LALRParser parser, TokenErrorEventArgs args) { failLineNumber = args.Token.Location.LineNr; failColumnNumber = args.Token.Location.ColumnNr; failLength = args.Token.Text.Length; failCategory = "Lexical Error"; failMessage = "'" + args.Token.ToString() + "'" + Environment.NewLine + "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + ", Length " + failLength + Environment.NewLine; }
/// <summary> /// Event handlers for the goto action. /// </summary> /// <param name="parser">parser that is the source of this event</param> /// <param name="args">parser that is the source of this event</param> public void GotoEvent(LALRParser parser, GotoEventArgs args) { AddViewItem("Goto", null, args.Symbol.ToString(), "", args.NewState.Id.ToString(), 3); }
/// <summary> /// Event handler for the shift action. /// </summary> /// <param name="parser">parser that is the source of this event</param> /// <param name="args">event arguments</param> private void ShiftEvent(LALRParser parser, ShiftEventArgs args) { AddViewItem("Shift", args.Token.Location, args.Token.Symbol.ToString(), StringUtil.ShowEscapeChars(args.Token.Text), args.NewState.Id.ToString(), 1); }
private void TokenErrorEvent(LALRParser parser, TokenErrorEventArgs args) { AddViewItem("Token error", args.Token.Location, "Cannot recognize token", args.Token.Text, "", 5); errors++; if (errors <= maxerrors) { args.Continue = true; } }
private void ParseErrorEvent(LALRParser parser, ParseErrorEventArgs args) { failLineNumber = args.UnexpectedToken.Location.LineNr; failColumnNumber = args.UnexpectedToken.Location.ColumnNr; failLength = args.UnexpectedToken.Text.Length; failCategory = "Syntax Error"; failMessage = "'" + args.UnexpectedToken.ToString() + "'" + Environment.NewLine + "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + ", Length " + failLength + Environment.NewLine + "Expecting one of: '" + args.ExpectedTokens.ToString() + "'"; }
public CalithaParser(LALRParser parser) // initialization of the GOLD parser from Resource tables is framework-dependent // this is if we want to generate objects during parsing via callbacks: see TextCalculator.CalcParserV1 //parser.OnReduce += new LALRParser.ReduceHandler(ReduceEvent); //parser.OnTokenRead += new LALRParser.TokenReadHandler(TokenReadEvent); //parser.OnAccept += new LALRParser.AcceptHandler(AcceptEvent); { parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); this.parser = parser; }
/// <summary> /// Event handler for the reduce action. /// </summary> /// <param name="parser">parser that is the source of this event</param> /// <param name="args">event arguments</param> private void ReduceEvent(LALRParser parser, ReduceEventArgs args) { //WriteLn(args.Rule.Id.ToString()); //WriteLn(args.Rule.Lhs.Id.ToString()); AddViewItem("Reduce", null, args.Token.Rule.ToString(), "", args.NewState.Id.ToString(), 2); }
public TablesForm(Syntactic syntactic, Semantic semantic) { _semantic = semantic; _syntactic = syntactic; _llanalyser = new LLParser(_semantic, _syntactic); _lrParser = new LRParser(_semantic, _syntactic); _slrParser = new SLRParser(_semantic, _syntactic); _lalrParser = new LALRParser(_semantic, _syntactic); InitializeComponent(); }
public void SearchFor(string searchTerm) { LALRParser lalrParser = GrammarReader.CreateNewParser(); _wiqlBuilder = new WiqlBuilder(); _fieldName = ""; _comparisonType = FieldComparison.Contains; lalrParser.OnTokenRead += new LALRParser.TokenReadHandler(lalrParser_OnTokenRead); lalrParser.OnParseError += new LALRParser.ParseErrorHandler(lalrParser_OnParseError); lalrParser.OnTokenError += new LALRParser.TokenErrorHandler(lalrParser_OnTokenError); lalrParser.Parse(searchTerm); }
private void ReduceEvent(LALRParser parser, ReduceEventArgs args) { try { args.Token.UserObject = CreateObject(args.Token); } catch (ApplicationException ex) { args.Continue = false; Logger.Log(DateTime.Now + ": " + ex.Message); //todo: Report message to UI? } }
public void Start(string input) { CGTReader reader = new CGTReader(cgt); parser = reader.CreateNewParser(); parser.OnTokenRead += OnRead; parser.OnShift += OnSF; parser.OnAccept += Parser_OnAccept; if (parser != null) { parser.Parse(input); } }
/// <summary> /// Only one needed per thread /// </summary> public Parser() { Errors = new List <string>(); FieldsNeeded = new SortedList <string, int>(32); ArrayParameter = Linq.Expression.Parameter(typeof(object[]), "Fields"); _Parser = _Reader.CreateNewParser(); _Parser.TrimReductions = true; _Parser.StoreTokens = LALRParser.StoreTokensMode.NoUserObject; _Parser.OnReduce += _Parser_OnReduce; _Parser.OnTokenRead += _Parser_OnTokenRead; _Parser.OnTokenError += _Parser_OnTokenError; _Parser.OnParseError += _Parser_OnParseError; }
/// <summary> /// Event handler for the reading a comment. /// </summary> /// <param name="parser">parser that is the source of this event</param> /// <param name="args">event arguments</param> private void CommentReadEvent(LALRParser parser, CommentReadEventArgs args) { string desc; if (args.LineComment) { desc = "Line Comment"; } else { desc = "Block Comment"; } AddViewItem("Comment Read", null, desc, args.Content, "", 6); }
public void OnRead(LALRParser parser, TokenReadEventArgs args) { current = new CLL_token(); current.location = args.Token.Location; switch (args.Token.Symbol.Name) { case "class id": current.type = Token_Type.id; break; } current.text = args.Token.Text; }
private void TokenReadEvent(LALRParser parser, TokenReadEventArgs args) { try { args.Token.UserObject = CreateObject(args.Token); } catch (ApplicationException ex) { args.Continue = false; tokenStack.Clear(); Logger.Log(DateTime.Now + ": " + ex.Message); throw ex; } }
private void AcceptEvent(LALRParser parser, AcceptEventArgs args) { NonterminalToken T = (NonterminalToken)args.Token; mContext.AnalysisCheckCodeInterface = this.AnalysisCheckCodeInterface; this.Context.AnalysisInterpreterHost = this; AnalysisRule program = AnalysisRule.BuildStatments(mContext, T); if (this.IsExecuteMode) { program.Execute(); } this.ProgramStart = null; }
private void AcceptEvent(LALRParser parser, AcceptEventArgs args) { NonterminalToken nonterminalToken = (NonterminalToken)args.Token; RuleContext.EnterCheckCodeInterface = this.EnterCheckCodeInterface; RuleContext.ClearState(); EnterRule program = EnterRule.BuildStatments(RuleContext, nonterminalToken); program.Execute(); if (RuleContext.IsVariableValidationEnable) { RuleContext.CheckCommandVariables(); } }
private void ParseErrorEvent(LALRParser parser, ParseErrorEventArgs args) { Context.ExpectedTokens = args.ExpectedTokens.ToString(); if (args.NextToken != null) { Context.NextToken = args.NextToken.ToString(); } if (args.UnexpectedToken != null) { Context.UnexpectedToken = args.UnexpectedToken.ToString(); } throw new ParseException(args); }
private void Init(Stream stream) { CGTReader reader = new CGTReader(stream); parser = reader.CreateNewParser(); parser.TrimReductions = false; parser.StoreTokens = LALRParser.StoreTokensMode.NoUserObject; parser.OnReduce += new LALRParser.ReduceHandler(ReduceEvent); parser.OnTokenRead += new LALRParser.TokenReadHandler(TokenReadEvent); parser.OnAccept += new LALRParser.AcceptHandler(AcceptEvent); parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); mContext = new Rule_Context(); }
public void OnSF(LALRParser parser, ShiftEventArgs args) { current = new CLL_token(); current.location = args.Token.Location; switch (args.Token.Symbol.Id) { case 3: current_ = 3; //current.type = Token_Type.id; break; } current.text = args.Token.Text; }
private void Init(Stream stream, IScope pScope = null) { CGTReader reader = new CGTReader(stream); parser = reader.CreateNewParser(); parser.StoreTokens = LALRParser.StoreTokensMode.NoUserObject; parser.TrimReductions = false; parser.OnReduce += new LALRParser.ReduceHandler(ReduceEvent); parser.OnTokenRead += new LALRParser.TokenReadHandler(TokenReadEvent); parser.OnAccept += new LALRParser.AcceptHandler(AcceptEvent); parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); /* * Configuration config = null; * * // check to see if config is already loaded * // if NOT then load default * try * { * config = Configuration.GetNewInstance(); * * if (config == null) * { * Configuration.Load(Configuration.DefaultConfigurationPath); * } * } * catch * { * // It may throw an error in the Configuration.GetNewInstanc() * // so try this from the default config path * Configuration.Load(Configuration.DefaultConfigurationPath); * }*/ if (pScope == null) { mContext = new Rule_Context(); } else { mContext = new Rule_Context(pScope); } }
void _Parser_OnTokenRead(LALRParser parser, TokenReadEventArgs args) { var token = args.Token; Func <Parser, TerminalToken, object> handler; if (Internal.SymbolRules.Handlers.TryGetValue((SymbolConstants)token.Symbol.Id, out handler)) { try { token.UserObject = handler(this, token); } catch (Exception ex) { Errors.Add(string.Format("{0}: {1}", args.Token.Symbol, ex.Message)); args.Continue = false; } } }
private void AcceptEvent(LALRParser parser, AcceptEventArgs args) { //System.Console.WriteLine("AcceptEvent "); NonterminalToken T = (NonterminalToken)args.Token; /* * try * { * Configuration.Load(Configuration.DefaultConfigurationPath); * this.Context.module = new MemoryRegion(); * } * catch (System.Exception ex) * { * Configuration.CreateDefaultConfiguration(); * Configuration.Load(Configuration.DefaultConfigurationPath); * this.Context.module = new MemoryRegion(); * }*/ mContext.EnterCheckCodeInterface = this.EnterCheckCodeInterface; mContext.ClearState(); EnterRule program = EnterRule.BuildStatments(mContext, T); program.Execute(); mContext.CheckAssignedVariables(); /* * if (this.IsExecuteMode && this.host.IsExecutionEnabled) * { * program.Execute(); * } * * if (mContext.DefineVariablesCheckcode != null) * { * * }*/ //this.ProgramStart //this.ProgramStart.Execute(); }
private void Init(Stream stream) { CGTReader reader = new CGTReader(stream); parser = reader.CreateNewParser(); parser.TrimReductions = false; parser.StoreTokens = LALRParser.StoreTokensMode.NoUserObject; parser.OnTokenError += new LALRParser.TokenErrorHandler(TokenErrorEvent); parser.OnParseError += new LALRParser.ParseErrorHandler(ParseErrorEvent); this.lowerCasePrimitiveTypeEnumValues = new List <string>(); this.autonumberedTypes = new List <PatternBufferType>(); foreach (PrimitiveType pt in Enum.GetValues(typeof(PrimitiveType))) { lowerCasePrimitiveTypeEnumValues.Add(pt.ToString().ToLower()); } this.error = null; }
private void ParseErrorEvent(LALRParser parser, ParseErrorEventArgs args) { AddViewItem("Parse error", null, "Expecting the following tokens:", args.ExpectedTokens.ToString(), "", 5); errors++; if (errors <= maxerrors) { args.Continue = ContinueMode.Skip; // example for inserting a new token /* * args.Continue = ContinueMode.Insert; * TerminalToken token = new TerminalToken((SymbolTerminal)parser.Symbols.Get(4), * "555", * new Location(0, 0, 0)); * args.NextToken = token; */ } }