Click here to Skip to main content
15,887,924 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to convert implicitly system.generics.list to system.generics.list? Pin
Sascha Lefèvre19-May-15 2:54
professionalSascha Lefèvre19-May-15 2:54 
GeneralRe: how to convert implicitly system.generics.list to system.generics.list? Pin
Engr Nouman19-May-15 3:10
Engr Nouman19-May-15 3:10 
GeneralRe: how to convert implicitly system.generics.list to system.generics.list? Pin
Sascha Lefèvre19-May-15 3:17
professionalSascha Lefèvre19-May-15 3:17 
QuestionHow to find COM port for a specific USB devices connected. Pin
Member 245846718-May-15 23:13
Member 245846718-May-15 23:13 
AnswerRe: How to find COM port for a specific USB devices connected. Pin
Simon_Whale19-May-15 1:33
Simon_Whale19-May-15 1:33 
AnswerRe: How to find COM port for a specific USB devices connected. Pin
Gerry Schmitz19-May-15 17:56
mveGerry Schmitz19-May-15 17:56 
AnswerRe: How to find COM port for a specific USB devices connected. Pin
Member 24584672-Jun-15 20:57
Member 24584672-Jun-15 20:57 
QuestionGrammar in GOLD for generic constructs Pin
Sascha Lefèvre17-May-15 10:08
professionalSascha Lefèvre17-May-15 10:08 
(No forum really fits..)

I'm trying to create a C#-like grammar in GOLD[^] that can handle generics with the same syntax like C# but the '<' is giving me trouble because the LALR-parser runs into a Shift-Reduce-conflict with the '<' used in the production for comparing values.

As I have seen in the C#-grammer-sample* that comes with GOLD you can do some acrobatics with the order of productions and whatnot to force the LALR-parser onto the right track (used for array-declarations there) but I can't figure out a way to make it work here.
* reflecting C# 1.0 I assume; my grammar is derived from it; the "acrobatics" are documented in the section titled "Statement Expressions & Local Variable Declaration"

I don't want to use a different parser because there's an extremely nifty framework (bsn[^]) to base your interpreter on which I'm already using - rather I'd use a different token than '<' to open a generic type argument list.

Here's the grammar, the culprit is on line 40/41 (section titled "Shared by multiple sections"). I realize due to its length it can be seen as a bit of an imposition. Please just don't bother if it doesn't really interest you Blush | :O
"Case Sensitive"    = 'True'
"Character Mapping" = 'Unicode'
"Start Symbol"      = <Compilation Unit>

! ----------------------------------------------------------------- Sets

{ID Head}           = {Letter}       + [_]
{ID Tail}           = {AlphaNumeric} + [_]
{String Ch}         = {Printable}    - ["]
{Lit String Ch}     = {All Valid}    - ["]
{Char Ch}           = {Printable}    - ['']
{Hex Digit}         = {Digit} + [abcdef] + [ABCDEF]
{Bin Digit}         = [01]
{Directive Ch}      = {All Valid} - {CR} - {LF}

! ----------------------------------------------------------------- Terminals

Identifier          =  {ID Head} {ID Tail}*
MemberName          = '.' {ID Head} {ID Tail}*
DecLiteral          = {Digit}+            ( [UuLl] | [Uu][Ll] | [Ll][Uu] )?
RealLiteral         = {Digit}*'.'{Digit}+ ( [dfmDFM] )?
StringLiteral       = '"'( {String Ch} | '\'{Printable} )* '"' | '@' ('"' {Lit String Ch}+ '"')+
CharLiteral         = '' ( {Char Ch} | '\'{Printable} )''

! ----------------------------------------------------------------- Comments

Comment Line        = '//'
Comment Start       = '/*'
Comment End         = '*/'

! ===========================================================================
! Shared by multiple sections
! ===========================================================================

<Valid ID>              ::= Identifier <Generic Type List Opt>
                         |  <Base Type>

<Qualified ID>          ::= <Valid ID> <Member List Opt>
     
<Member List Opt>       ::= <Member List Opt> MemberName <Generic Type List Opt>
                         |  !Zero or more  

<Generic Type List Opt> ::= '<' <Generic Type List> '>'
                         |

<Generic Type List>     ::= <Generic Type List> ',' <Generic Type> 
                         |  <Generic Type>

<Generic Type>          ::= <Qualified ID>

! ===========================================================================
! Literals 
! ===========================================================================

<Literal>               ::= DecLiteral
                         |  RealLiteral
                         |  CharLiteral
                         |  StringLiteral
                         |  true
                         |  false
                         |  null
     
! ===========================================================================
! Types 
! ===========================================================================

! The following defines built-in datatypes only. This is necessary for local
! variable declarations.

<Base Type>             ::= <Other Type>
                         |  <Integral Type>

<Other Type>            ::= float
                         |  double
                         |  decimal
                         |  bool
                         |  object
                         |  string
                         |  datetime
                         |  var
 
! Integral types are valid in enumeration declarations.

<Integral Type>         ::= sbyte 
                         |  byte 
                         |  short
                         |  ushort
                         |  int
                         |  uint 
                         |  long 
                         |  ulong
                         |  char

! ===========================================================================
! C.2.4 Expressions 
! ===========================================================================

<Expression Opt>        ::= <Expression>
                         |  !Nothing 

<Expression List>       ::= <Expression> ',' <Expression List>
                         |  <Expression>

<Expression>            ::= <Conditional Exp> '='   <Expression>
                         |  <Conditional Exp> '+='  <Expression>
                         |  <Conditional Exp> '-='  <Expression>
                         |  <Conditional Exp> '*='  <Expression>
                         |  <Conditional Exp> '/='  <Expression>
                         |  <Conditional Exp>

<Conditional Exp>       ::= <Or Exp> '?' <Or Exp> ':' <Conditional Exp>
                         |  <Or Exp>

<Or Exp>                ::= <Or Exp> '||' <And Exp>
                         |  <And Exp>

<And Exp>               ::= <And Exp> '&&' <Equality Exp>
                         |  <Equality Exp>

<Equality Exp>          ::= <Equality Exp> '==' <Compare Exp>
                         |  <Equality Exp> '!=' <Compare Exp>
                         |  <Compare Exp>

<Compare Exp>           ::= <Compare Exp> '<'  <Add Exp>
                         |  <Compare Exp> '>'  <Add Exp>
                         |  <Compare Exp> '<=' <Add Exp>
                         |  <Compare Exp> '>=' <Add Exp>
!                         |  <Compare Exp> is <Qualified ID>       ! <<<<< Shift-Reduce-Conflict
!                         |  <Compare Exp> as <Qualified ID>       ! <<<<< Shift-Reduce-Conflict
                         |  <Add Exp>

<Add Exp>               ::= <Add Exp> '+' <Mult Exp>
                         |  <Add Exp> '-' <Mult Exp>
                         |  <Mult Exp>

<Mult Exp>              ::= <Mult Exp> '*' <Unary Exp>  
                         |  <Mult Exp> '/' <Unary Exp>  
                         |  <Mult Exp> '%' <Unary Exp>  
                         |  <Unary Exp>  

<Unary Exp>             ::= '!'  <Unary Exp>
                         |  '-'  <Unary Exp>
                         |  '++' <Unary Exp>
                         |  '--' <Unary Exp>
                         |  '(' <Expression> ')' <Method Exp>     ! Cast "expression" is required to avoid a conflict
                         |  <Method Exp>

<Method Exp>            ::= <Method Exp> <Method>
                         |  <Primary Exp>

<Primary Exp>           ::= new <Qualified ID> '(' <Arg List Opt> ')'     ! Non array creation
                         |  '(' <Expression> ')' 
                         |  <Primary>
       
<Primary>               ::= <Valid ID> '(' <Arg List Opt> ')'       ! Current object method 
                         |  <Literal>
!                         |  <Valid ID>  ! replaced by <Base Type> / Identifier
                         |  <Base Type>
!                         |  Identifier                             ! <<<<< Shift-Reduce-Conflict

! ===========================================================================
! Arguments
! ===========================================================================

<Arg List Opt>          ::= <Arg List>
                         |  !Nothing
       
<Arg List>              ::= <Argument> ',' <Arg List>
                         |  <Argument>

<Argument>              ::= <Expression>
            
! ===========================================================================
! C.2.5 Statements 
! ===========================================================================

<Stm List>              ::= <Statement> <Stm List>
                         |  <Statement>

! This repetative productions below resolve the hanging-else problem by 
! restricting the "if-then" statement to remove ambiguity. Two levels of 
! statements are declared with the second, "restricted", group only used in
! the "then" clause of a "if-then-else" statement. 
!
! The "restricted" group is completely identical the the first with one 
! exception: only the "if-then-else" variant of the if statement is allowed. 
! In other words, no "if" statements without "else" clauses can appear inside
! the "then" part of an "if-then-else" statement. Using this solution, the 
! "else" will bind to the last "If" statement, and still allows chaining.

<Statement>             ::= <Local Var Decl> ';'
                         |  if       '(' <Expression> ')' <Statement>
                         |  if       '(' <Expression> ')' <Then Stm> else <Statement>        
                         |  for      '(' <For Init Opt> ';' <For Condition Opt> ';' <For Iterator Opt> ')' <Statement>
                         |  while    '(' <Expression> ')' <Statement>
                         |  <Normal Stm>

<Then Stm>              ::= if       '(' <Expression> ')' <Then Stm> else <Then Stm>        
                         |  for      '(' <For Init Opt> ';' <For Condition Opt> ';' <For Iterator Opt> ')' <Then Stm>
                         |  while    '(' <Expression> ')' <Then Stm>       
                         |  <Normal Stm>   
          
<Normal Stm>            ::= try <Block> <Catch Clauses Opt> <Finally Clause Opt>        
                         |  break ';'
                         |  continue ';'
                         |  return <Expression Opt> ';'
                         |  display <Expression> ';'
                         |  <Statement Exp> ';'        
                         |  <Block>    

<Block>                 ::= '{' <Stm List> '}'
                         |  '{' '}' 

<Variable Declarator>   ::= Identifier
                         |  Identifier '=' <Expression>

! ===========================================================================
! For Clauses
! ===========================================================================

<For Init Opt>          ::= <Local Var Decl>
                         |  <Statement Exp List>
                         |  !Nothing

<For Iterator Opt>      ::= <Statement Exp List>
                         |  !Nothing 

<For Condition Opt>     ::= <Expression>
                         |  !Nothing 

<Statement Exp List>    ::= <Statement Exp> ',' <Statement Exp List>
                         |  <Statement Exp>

! ===========================================================================
! Catch Clauses
! ===========================================================================

<Catch Clauses Opt>     ::= <Catch Clause> <Catch Clauses Opt>
                         |  !Nothing

<Catch Clause>          ::= catch '(' <Qualified ID> Identifier ')' <Block>

<Finally Clause Opt>    ::= finally <Block>
                         |  !Nothing

! ===========================================================================
! Statement Expressions & Local Variable Declaration
! ===========================================================================

! The complex productions below are able to avoid the shift-reduce error caused
! by declaring an array. The notation used by C# (and the rest of the C++
! family) prevents an array declaration to be distinguished from an array 
! assignment statement until a number of characters are read.
!
! a.b.c[2] = "Test"
! a.b.c[] = new String[3]
!
! The system CANNOT make a decision between the two until it is reading the 
! contents the [ ... ]. 
!
! As a result, the local variable declaration below contains the full notation
! for each of the C# methods at the same level as local variable declarations. 
! Since the system does not have to reduce UNTIL it is within the [ ... ], no
! shift-reduce error will occur. Nasty, huh?

<Local Var Decl>        ::= <Qualified ID> <Variable Declarator> 

<Statement Exp>         ::= <Qualified ID> '(' <Arg List Opt> ')'
                         |  <Qualified ID> '(' <Arg List Opt> ')'    <Methods Opt> <Assign Tail>          
                         |  <Qualified ID> '[' <Expression List> ']' <Methods Opt> <Assign Tail> 
                         |  <Qualified ID>                                         <Assign Tail>

<Assign Tail>           ::= '++'
                         |  '--'       
                         |  '='   <Expression>
                         |  '+='  <Expression>
                         |  '-='  <Expression>
                         |  '*='  <Expression>
                         |  '/='  <Expression>

<Methods Opt>           ::= <Method> <Methods Opt>
                         |  !Null

<Method>                ::= MemberName ! <Generic Type List Opt>                          ! <<<<< Shift-Reduce-Conflict
!                         |  MemberName <Generic Type List Opt> '(' <Arg List Opt> ')'    ! <<<<< Shift-Reduce-Conflict
                         |  '[' <Expression List> ']'

! ===========================================================================
! C.2.6 Namespaces
! ===========================================================================

<Compilation Unit>      ::= <Using List Opt> <Compilation Items Opt>

<Using List Opt>        ::= <Using Directive> <Using List Opt>
                         |  !Nothing

<Using Directive>       ::= using <Qualified ID> ';'

<Compilation Items Opt> ::= <Compilation Item> <Compilation Items Opt>
                         |  ! Zero or more

<Compilation Item>      ::= <Method Dec>
                         |  <Statement>
!                         | '{' <Stm List> '}'

! ===========================================================================
! C.2.7 Classes
! ===========================================================================

<Method Dec>            ::= <Qualified ID> Identifier <Generic Type List Opt> '(' <Formal Param List Opt> ')' <Block>
                         |          'void' Identifier <Generic Type List Opt> '(' <Formal Param List Opt> ')' <Block>
        
<Formal Param List Opt> ::= <Formal Param List>
                         |  !Nothing

<Formal Param List>     ::= <Formal Param> ',' <Formal Param List>
                         |  <Formal Param>

<Formal Param>          ::= <Qualified ID> Identifier

edit: grammar updated
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson


modified 23-May-15 13:55pm.

AnswerRe: Grammar in GOLD for generic constructs Pin
Kenneth Haugland18-May-15 23:27
mvaKenneth Haugland18-May-15 23:27 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre19-May-15 1:55
professionalSascha Lefèvre19-May-15 1:55 
AnswerRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 0:27
professionalManfred Rudolf Bihy19-May-15 0:27 
AnswerRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 0:36
professionalManfred Rudolf Bihy19-May-15 0:36 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre19-May-15 1:51
professionalSascha Lefèvre19-May-15 1:51 
GeneralRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 4:54
professionalManfred Rudolf Bihy19-May-15 4:54 
GeneralRe: Grammar in GOLD for generic constructs Pin
Brisingr Aerowing19-May-15 6:12
professionalBrisingr Aerowing19-May-15 6:12 
GeneralRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 9:28
professionalManfred Rudolf Bihy19-May-15 9:28 
GeneralRe: Grammar in GOLD for generic constructs Pin
Brisingr Aerowing19-May-15 17:28
professionalBrisingr Aerowing19-May-15 17:28 
GeneralRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 5:34
professionalManfred Rudolf Bihy19-May-15 5:34 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre19-May-15 7:01
professionalSascha Lefèvre19-May-15 7:01 
GeneralRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy19-May-15 8:27
professionalManfred Rudolf Bihy19-May-15 8:27 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre19-May-15 8:37
professionalSascha Lefèvre19-May-15 8:37 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre20-May-15 2:46
professionalSascha Lefèvre20-May-15 2:46 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre23-May-15 8:04
professionalSascha Lefèvre23-May-15 8:04 
GeneralRe: Grammar in GOLD for generic constructs Pin
Sascha Lefèvre8-Jun-15 20:20
professionalSascha Lefèvre8-Jun-15 20:20 
GeneralRe: Grammar in GOLD for generic constructs Pin
Manfred Rudolf Bihy8-Jun-15 20:28
professionalManfred Rudolf Bihy8-Jun-15 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.