-
Notifications
You must be signed in to change notification settings - Fork 3
TokenSet, First(), Follow() #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
dffb119
918822b
ee593ed
8fcfa66
0920cbe
dfee09a
3f81c9d
61a32f4
24a7263
be294dc
e9f9daa
5e24145
6498963
8c2983c
95b4e53
1bb9c75
174cb31
39198dd
914bc6d
e04144b
26f5b85
a14c66c
4c63fa4
eb26eb0
27bbaf7
f2c61fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* M2Sharp -- Modula-2 to C# Translator & Compiler | ||
| * | ||
| * Copyright (c) 2016 The Modula-2 Software Foundation | ||
| * | ||
| * Author & Maintainer: Benjamin Kowarsch <trijezdci@org.m2sf> | ||
| * | ||
| * @synopsis | ||
| * | ||
| * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. | ||
| * It supports the dialects described in the 3rd and 4th editions of Niklaus | ||
| * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, | ||
| * and an extended mode with select features from the revised language by | ||
| * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). | ||
| * | ||
| * In translator mode, M2Sharp translates Modula-2 source to C# source files. | ||
| * In compiler mode, M2Sharp compiles Modula-2 source via C# source files | ||
| * to object code or executables using the host system's C# compiler. | ||
| * | ||
| * @repository | ||
| * | ||
| * https://github.com/m2sf/m2sharp | ||
| * | ||
| * @file | ||
| * | ||
| * NonTerminals.cs | ||
| * | ||
| * provides FIRST() and FOLLOW() sets for each non-terminal symbol //SAM CHANGE | ||
| * used by the parser class for syntax analysis | ||
| * | ||
| * @license | ||
| * | ||
| * M2Sharp is free software: you can redistribute and/or modify it under the | ||
| * terms of the GNU Lesser General Public License (LGPL) either version 2.1 | ||
| * or at your choice version 3 as published by the Free Software Foundation. | ||
| * However, you may not alter the copyright, author and license information. | ||
| * | ||
| * M2Sharp is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with M2Sharp. If not, see <https://www.gnu.org/copyleft/lesser.html>. | ||
| * | ||
| * NB: Components in the domain part of email addresses are in reverse order. | ||
| */ | ||
|
|
||
| namespace org.m2sf.m2sharp { | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class NonTerminals : INonTerminals { | ||
|
|
||
| public static Production firstConstParamDependent = Production.FormalType, | ||
| lastConstParamDependent = Production.AttribFormalParams, | ||
| firstNoVariantRecDependent = Production.TypeDeclarationTail, | ||
| lastNoVariantRecDependent = Production.TypeDeclarationTail, | ||
| firstOptionDependent = firstConstParamDependent, | ||
| lastOptionDependent = lastNoVariantRecDependent; | ||
| public static int alternateSetOffset = (int)lastOptionDependent - (int)firstOptionDependent + 1; | ||
|
|
||
| /* -------------------------------------------------------------------------- | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a private array with the initialisers for all FIRST and FOLLOW sets here. Ideally all initialisation will take place at compile time. The FIRST() and FOLLOW() functions should then compute the index of the appropriate set in the private array, fetch it from the array and return it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where to pull the values for the first and follow sets from. It's possible I'm just completely missing something obvious. Can you point me where I need to be?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to write standalone programs that generate the code. The C versions are https://bitbucket.org/trijezdci/m2c-rework/src/tip/gen_first_sets.c The C versions of the generated code are https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-first-set-inits.h In C we pull them in via #include directives, but in C# there is no such thing. We could use templates but for now let's simply copy-paste the generated code in. |
||
| * method Count() -- Returns the number of productions | ||
| * ----------------------------------------------------------------------- */ //SAM CHANGE | ||
|
|
||
| uint Count() { | ||
| uint total = 0; | ||
| foreach (String s in Enum.GetNames(typeof(Production))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In C# we can use the
|
||
| total++; | ||
| } | ||
| return total; | ||
| } /* end Count */ | ||
|
|
||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method IsOptionDependent(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns true if p is dependent on any compiler option, else false. | ||
| * ----------------------------------------------------------------------- */ //SAM CHANGE | ||
|
|
||
| bool IsOptionDependent(Production p) { | ||
| return p >= firstOptionDependent; | ||
| } /* end IsOptionDependent */ | ||
|
|
||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method IsConstParamDependent(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns true if p is dependent on CONST parameter option, else false. | ||
| * ----------------------------------------------------------------------- */ //SAM CHANGE | ||
|
|
||
| bool IsConstParamDependent(Production p) | ||
| { | ||
| return p >= firstConstParamDependent && p <= lastConstParamDependent; | ||
| } /* end IsConstParamDependent */ | ||
|
|
||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method IsVariantRecordDependent(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns true if p is dependent on variant record type option, else false. | ||
| * ----------------------------------------------------------------------- */ //SAM CHANGE | ||
|
|
||
| bool IsVariantRecordDependent(Production p) { | ||
| return p >= firstNoVariantRecDependent && p <= lastNoVariantRecDependent; | ||
| } | ||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method FIRST(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns a tokenset with the FIRST set of production p. | ||
| * ----------------------------------------------------------------------- */ | ||
|
|
||
| TokenSet FIRST(Production p) { | ||
| TokenSet tokenset = null; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add |
||
| if (IsConstParamDependent(p)) | ||
| { | ||
| p += alternateSetOffset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a particular reason you like Convert.ToUInt32(p) over (uint)p? |
||
| } /* end if */ | ||
| if (IsVariantRecordDependent(p) && CompilerOptions.VariantRecords()) | ||
| { | ||
| p += alternateSetOffset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as for line 119 |
||
| } /* end if */ | ||
|
|
||
| return tokenset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This always returns null. We need initialisers for all the FIRST sets, ideally in an array where the index matches the value of see once we have the initialisers, we can get the set from the array index = Convert.ToUInt32(p) + alternateSetOffset;
...
tokenset = firstSet[index];
... |
||
| } /* end FIRST */ | ||
|
|
||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method FOLLOW(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns a tokenset with the FOLLOW set of production p. | ||
| * ----------------------------------------------------------------------- */ | ||
|
|
||
| TokenSet FOLLOW(Production p) { | ||
| TokenSet tokenset = null; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see line 116 |
||
| if (IsConstParamDependent(p)) { | ||
| p += alternateSetOffset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see line 119 |
||
| } /* end if */ | ||
| if (IsVariantRecordDependent(p) && !CompilerOptions.VariantRecords()) { | ||
| p += alternateSetOffset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see line 119 |
||
| } /* end if */ | ||
|
|
||
| return tokenset; | ||
| } /* end FOLLOW */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This too, always returns null. We need initialisers for all the FOLLOW sets, in the same manner as for the FIRST sets. see and accordingly ... index = Convert.ToUInt32(p) + alternateSetOffset;
...
tokenset = followSet[index];
... |
||
|
|
||
|
|
||
| /* -------------------------------------------------------------------------- | ||
| * method NameForProduction(p) | ||
| * -------------------------------------------------------------------------- | ||
| * Returns a string with a human readable name for production p. | ||
| * ----------------------------------------------------------------------- */ | ||
|
|
||
| string NameForProduction(Production p); | ||
|
|
||
|
|
||
|
|
||
| } /* NonTerminals */ | ||
|
|
||
| } /* namespace */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please follow the original formatting: