Line number problem with the Synthesizer Generator.

vdberg@cs.utwente.nl (Klaas van de Berg)
Fri, 1 Jul 1994 15:13:18 GMT

          From comp.compilers

Related articles
Line number problem with the Synthesizer Generator. vdberg@cs.utwente.nl (1994-07-01)
| List of all articles for this month |

Newsgroups: comp.compilers
From: vdberg@cs.utwente.nl (Klaas van de Berg)
Keywords: tools, question
Organization: Compilers Central
Date: Fri, 1 Jul 1994 15:13:18 GMT

I have encountered the following problem with line numbers
in a calculator (a simple version of my original problem).
Besides the value of an expression I want to obtain
the line number of each constant in the expression in the input.


(e.g. Reps & Teitelbaum, 1989, The Synthesizer Generator, Ch 6
  or: GrammaTech, 1993, The Synthesizer Generator Reference Manual,
                                                appendix A)




For example, I have a file with the following content:
  7


  +
  8


This should result in the following output:


(7 (at lnr:1) + 8 (at lnr:4))
VALUE = 15


The output shows the expression and the line number in the input file
for each constant, and the value of the expression.


It is clear to me that I should get the numbers at parse time.
The function lines_read() seems appropriate.
However, I have not been successful until now.
The code of one of my unsuccessful attempts has been given below
(with the line numbers calculated after parsing the input file)




I will appreciate any suggestions in solving this problem.




Klaas van den Berg
University Twente
Enschede
the Netherlands
e-mail vdberg@cs.utwente.nl








***********************************************************************


#include "user_interface_exp.h"


FOREIGN LinesRead()
{
      return Int(lines_read());
}


INT foreign LinesRead();


/* abstract syntax */


root calc;
list calc;
calc : CalcNil()
          | CalcPair(exp calc)
          ;
exp : Null()
          | Sum(exp exp)
          | Const(INT)
          ;


/* attribute declarations */


exp {syn INT v;
            syn INT lnr;};


exp: Null { exp.v = 0;
                            exp.lnr = 0;}
      | Sum { exp$1.v = exp$2.v + exp$3.v;
                            exp$1.lnr = exp$2.lnr;}
      | Const { exp$1.v = INT;
                            exp$1.lnr = LinesRead();} /* wrong place */
      ;


/* unparsing declarations */


calc : CalcPair [ @ ::= @ "%nVALUE = "exp.v [";%n%n"] @ ];
exp : Null [ @ ::= "<exp>"]
            | Sum [ @ ::= "(" @ " + " @ ")"]
            | Const [ @ ::= ^ " (at lnr:" exp.lnr ")" ]
            ;


/* transformation declarations */


transform exp
      on "+" <exp>: Sum( <exp>,<exp>)
      ;


/* lexemes declarations */


INTEGER : Integer < [0-9]+ >;
WHITESPACE : Whitespace < [\ \t\n] >;
VALUE : Value < "VALUE" >;


/* parsing declarations */


Calc { syn calc abs; };
Exp { syn exp abs; };


Calc ::= (Exp) { Calc.abs = CalcPair(Exp.abs, CalcNil()); }
                | (Exp Calc) { Calc$1.abs = CalcPair(Exp.abs, Calc$2.abs); }
                ;


Exp ::= (INTEGER) { $$.abs = Const(STRtoINT(INTEGER));}
                | (Exp '+' Exp) { $$.abs = Sum(Exp$2.abs, Exp$3.abs);}
                ;


calc ~ Calc.abs;
exp ~ Exp.abs;


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.