'QUT.Gppg.ShiftReduceParser<,>' does not contain a constructor that takes '0' arguments

Jan 23, 2010 at 6:34 PM

Hi everyone,

 

First, thanks for writing such a useful tool (together with GPLex).

 

Second ... I'm having this error when trying to compile the project containing the generated parser.  Currently, nothing outside the ShiftReduceParser and GPPG generated code refers to ShiftReduceParser or Parser.  I'm using Visual Studio 2008 Pro, with .Net 3.5, and the GPPG version is 1.3.6.  The error happens whether I use the DLL for ShiftReduceParser, or add the code file to my project.

It's not a problem for me, because I think there's a workaround (add a constructor); but it does mean that GPPG parsers won't compile in my environment without a tweak.  Anyway, I thought I'd share it here.

Details:

Error message:

Error    1    'QUT.Gppg.ShiftReduceParser<PaintedHorse.Leaf.Expression.SyntaxTree,QUT.Gppg.LexLocation>' does not contain a constructor that takes '0' arguments    parser.cs    34    24

And the top portion of parser.cs is:

// This code was generated by the Gardens Point Parser Generator
// Copyright (c) Wayne Kelly, QUT 2005-2008
// (see accompanying GPPGcopyright.rtf)

// GPPG version 1.3.6
// Machine:  NEO
// DateTime: 23/01/2010 18:58:54
// UserName: anyone
// Input file <expression.y>

// options: no-lines gplex

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using QUT.Gppg;

namespace PaintedHorse.Leaf.Expression
{
internal enum Tokens {
    error=1,EOF=2,Identifier=3,IntLiteral=4,FloatLiteral=5,StringLiteral=6,
    OtherLiteral=7,LeftBracket=8,RightBracket=9,Comma=10,Precedence13=11,Precedence12=12,
    Precedence11=13,Precedence10=14,Precedence09=15,Precedence08=16,Precedence07=17,Precedence06=18,
    Precedence05=19,Precedence04=20,Precedence03=21,Precedence02=22,Precedence01=23,Precedence00=24};

// Abstract base class for GPLEX scanners
internal abstract class ScanBase : AbstractScanner<SyntaxTree,LexLocation> {
  private LexLocation __yylloc = new LexLocation();
  public override LexLocation yylloc { get { return __yylloc; } set { __yylloc = value; } }
  protected virtual bool yywrap() { return true; }
}

internal partial class Parser: ShiftReduceParser<SyntaxTree, LexLocation>
{

// ---- snippety snip ----

 

My interpretation of this is that ShiftReduceParser doesn't include a default (zero argument) constructor; Parser doesn't explicitly include any constructors, so gets the default one, which means that it's expecting its base class to have one.  It all compiles happily if I explicitly add a zero argument constructor to ShiftReduceParser:

protected ShiftReduceParser()
 {
 }

 

Thanks in advance for any light anyone can shed ...

 

Chris Owens.

 

Jan 24, 2010 at 8:20 AM

I'm going to answer my own question here:

You're expected to write your own constructor for the class Parser, which should then call the constructor for the base class.  So, for example (assuming that you've run gppg with the %partial option and have an auxiliary file with additional content for the class Parser), you might put this in the auxiliary file:

        internal Parser(Scanner lex): base(lex)
        {
             // Whatever other initialization actions the parser needs 
        }

I think the original poster must be some kind of idiot.

Chris Owens.

Coordinator
Jan 24, 2010 at 1:15 PM

Hi Folks

Umm, shouldn't be unkind to other posters Chris <grin>

Yes, see the section 1.6 "Instantiating the Parser Object" in the documentation.  This is page 7 of the new format doco available as a separate download from CodePlex.  Featuring a proper table of contents and an index.

 

Jan 16, 2012 at 11:44 AM

I think that the question is still valid.

The normal generated Parser class is not partial and doesn't have proper constructor. This makes the generated code unusable, because it cannot be copiled. Why would gppg have the %partial directive if the generated code cannot be compiled without it?

Why not generate the proper constructor when the %partial directive is not present?

BTW, the samples from the latest distribution (1.4.5) obviously don't build:

csc.exe /r:QUT.ShiftReduceParser.dll RealCalc.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.17020
Copyright (C) Microsoft Corporation. All rights reserved.
RealCalc.cs(159,25): error CS0103: The name 'scanner' does not exist in the
        current context
RealCalc.cs(204,12): error CS1061: 'Parser' does not contain a definition for
        'scanner' and no extension method 'scanner' accepting a first argument
        of type 'Parser' could be found (are you missing a using directive or an
        assembly reference?)
RealCalc.cs(25,14): error CS1729:
        'QUT.Gppg.ShiftReduceParser' does not
        contain a constructor that takes 0 arguments
QUT.ShiftReduceParser.dll: (Location of symbol related to previous error)
Jan 16, 2012 at 12:12 PM

Well I figured this out (the code blocks are placed inside the Parser class).

The sample should seriously be fixed - it's very misleading. And I think that an inherited constructor is still wanted.

Coordinator
Jan 20, 2012 at 3:01 AM

The code of RealCalc.y was not compatible with the recent versions of gppg.

This is now fixed in release V 1.4.6, along with some other stuff.  The text of RealCalc.y explains more about the issue of constructors for the parser class.