Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions NonTerminals.cs
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;
Copy link
Copy Markdown
Member

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:

  • strictly NO TABs, only spaces.
  • two spaces indentation.
  • maximum 79 characters per line

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;

/* --------------------------------------------------------------------------
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
https://bitbucket.org/trijezdci/m2c-rework/src/tip/gen_follow_sets.c

The C versions of the generated code are

https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-first-set-inits.h
https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-follow-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))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C# we can use the Length() method to get the number of items in an enumeration:

Enum.GetNames(typeof(Production)).Length;

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add uint index = 0;

if (IsConstParamDependent(p))
{
p += alternateSetOffset;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be index = Convert.ToUInt32(p) + alternateSetOffset;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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?
Should run a find & replace and change all of the code to one or the other?

} /* end if */
if (IsVariantRecordDependent(p) && CompilerOptions.VariantRecords())
{
p += alternateSetOffset;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as for line 119

} /* end if */

return tokenset;
Copy link
Copy Markdown
Member

@trijezdci trijezdci May 19, 2017

Choose a reason for hiding this comment

The 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 p.

see
https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-production.c#m2-production.c-48
https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-production.c#m2-production.c-231

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see line 116

if (IsConstParamDependent(p)) {
p += alternateSetOffset;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see line 119

} /* end if */
if (IsVariantRecordDependent(p) && !CompilerOptions.VariantRecords()) {
p += alternateSetOffset;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see line 119

} /* end if */

return tokenset;
} /* end FOLLOW */
Copy link
Copy Markdown
Member

@trijezdci trijezdci May 19, 2017

Choose a reason for hiding this comment

The 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
https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-production.c#m2-production.c-139
https://bitbucket.org/trijezdci/m2c-rework/src/tip/m2-production.c#m2-production.c-325

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 */
Loading