head	1.1;
access;
symbols;
locks
	gtoal:1.1; strict;
comment	@# @;


1.1
date	2022.02.05.08.49.51;	author gtoal;	state Exp;
branches;
next	;


desc
@grammar
@


1.1
log
@Initial revision
@
text
@# Grammar for Imp80 on Emas-2900 series.
# based on https://history.dcs.ed.ac.uk/archive/os/emas/emas2/compilers/imp80/imp80ps04.txt

B<NAME>=1;
B<CONST>=2;
B<NOTECONST>=3;
B<DUMMYAPP>=4;
B<HOLE>=5;
B<MARK>=6;
B<OP>=7;
B<TEXTTEXT>=8;
B<CHXTYPE>=9;
B<UP>=10;
B<DOWN>=11;
B<LISTOFF>=12;
B<LISTON>=13;
B<COMP1>=14;
B<COMP2>=15;
B<ASSOP>=16;
B<BIGHOLE>=17;
B<READLINEq>=18;
B<COLON>=19;
B<S>=20;
B<NOTESTART>=21;
B<ICONST>=22;
B<SETNEM>=23;
B<N255>=24;
B<UCWRONG>=25;
B<PRIMFORM>=26;
B<SECNFORM>=27;
B<TERTFORM>=28;
B<N>=29;
B<CHUI>=30;
B<TEXT>=31;
B<NOTEFINISH>=32;
B<NOTECYCLE>=33;
B<NOTEREPEAT>=34;
B<INCLUDE>=35;
B<TRACE>=36;
B<DUMMYSTART>=37;
B<EOF>=38;
B<char>=39;

P<Opt-N>=
  <N>,
  ;

P<Opt-PLUS>=
  '+',
  '-',
  ;

P<OPERAND>=
  <NAME><APP><Opt-ENAME>,
  <CONST><NOTECONST>,
  '('<Opt-EXPR>')';

# Constant operand
P<COPERAND>=
  <NAME><DUMMYAPP>,
  <CONST>,
  '('<Opt-CEXPR>')';

# Constant expression
P<CEXPR>=
  <HOLE><Opt-PLUS><COPERAND><MARK><RESTOFCEXPR>;

P<Opt-CEXPR>=
  <CEXPR>,
  ;

# Used in switch label.  (*) marks a catch-all. (like default: in C)
P<STAROREXPR>=
  <HOLE><Opt-PLUS><COPERAND><MARK><RESTOFCEXPR>,
  '*';

P<Opt-STAROREXPR>=
  <STAROREXPR>,
  ;

P<EXPR>=
  <HOLE><Opt-PLUS><OPERAND><MARK><RESTOFEXPR>;

P<Opt-EXPR>=
  <EXPR>,
  ;

P<RESTOFEXPR>=
  <OP><OPERAND><RESTOFEXPR>,
  ;

P<RESTOFCEXPR>=
  <OP><COPERAND><RESTOFCEXPR>,
  ;

# 'Application' i.e. parameters to routine or function call
P<APP>=
  '('<EXPR><RESTOFAPP>')',
  ;

P<RESTOFAPP>=
  ','<EXPR><RESTOFAPP>,
  ;

P<Percent-IU>=
  "if",
  "unless";

P<Percent-WU>=
  "while"<HOLE><SC><RESTOFCOND><MARK>,
  "until"<HOLE><SC><RESTOFCOND><MARK>,
  "for"<HOLE><NAME>'='<EXPR>','<EXPR>','<EXPR><MARK>;

P<Opt-ALIAS>=
  "alias"<TEXTTEXT>,
  ;

P<Opt-NLIST>=
  ','<NAME><Opt-NLIST>,
  ;

P<FULLTYPE>=
  "integer",
  ;

P<BTYPE>=
  "real",
  "integer",
  "long" "real";

P<XTYPE>=
  "integer",
  "real",
  "long"<BTYPE>,
  "byte"<FULLTYPE>,
  "string"<REPFACT>,
  "half"<FULLTYPE>,
  "record"'('<Opt-RFREF>')';

P<RT>=
  "routine",
  <CHXTYPE><XTYPE><FM>;

P<FM>=
  "fn",
  "map",
  "function";

P<FPDEL>=
  <XTYPE><Opt-PercentQNAME><NAME><Opt-NLIST>,
  <RT><Opt-PercentNAME><NAME><Opt-NLIST><FPP>,
  "name"<NAME><Opt-NLIST>;

P<Opt-PercentNAME>=
  "name",
  ;

P<Opt-PercentQNAME>=
  "array" "name",
  "name",
  ;

P<FPP>=
  '('<HOLE><FPDEL><MARK><RESTOFFPLIST>')',
  ;

P<RESTOFFPLIST>=
  <Opt-Comma><HOLE><FPDEL><MARK><RESTOFFPLIST>,
  ;

P<Opt-Comma>=
  ',',
  ;

P<ENDLIST>=
  "of" "program"<UP>,
  "of" "file",
  "of" "list"<LISTOFF>,
  <UP>;

P<Opt-PercentFORMAT>=
  "format",
  ;

# RestOfSc handles double-sided conditions such as '0' <= ch <= '9'
P<SC>=
  <EXPR><COMP1><EXPR><RESTOFSC>,
  '('<SC><RESTOFCOND>')',
  "not"<SC>;

P<RESTOFSC>=
  <COMP2><EXPR>,
  ;

# Imp does not let you mix and/or without explicit brackets. They do not have a precedence relationship
P<RESTOFCOND>=
  "and"<SC><RESTOFANDC>,
  "or"<SC><RESTOFORC>,
  ;

P<RESTOFANDC>=
  "and"<SC><RESTOFANDC>,
  ;

P<RESTOFORC>=
  "or"<SC><RESTOFORC>,
  ;

P<RESTOFUI>=
  <ASSOP><EXPR>,
  ':',
  ;

P<Opt-PercentSPEC>=
  "spec"<BIGHOLE>,
  <DOWN>;

P<Opt-VSPEC>=
  "spec",
  ;

# List of lower/upper bound pairs, for array declarations.
P<RESTOFBPLIST>=
  ','<EXPR>':'<EXPR><RESTOFBPLIST>,
  ;

# Scalar declaration
P<DECLN>=
  <Opt-PercentQNAME><NAME><Opt-NLIST>,
  "array"<Opt-PercentFORMAT><ADECLN>;

# Array declaration
P<ADECLN>=
  <NAME><Opt-NLIST><BPAIR><RESTOFARLIST>;

# Multi-dimensional arrays
P<RESTOFARLIST>=
  ','<ADECLN>,
  ;

P<OWNDEC>=
  <Opt-PercentQNAME><Opt-VSPEC><HOLE><NAME><Opt-ALIAS><Opt-CONST><MARK><RESTOFOWNDEC>,
  "array"<Opt-PercentFORMAT><Opt-VSPEC><NAME><BPAIR><CONSTLIST> ;

P<RESTOFOWNDEC>=
  ','<HOLE><NAME><Opt-ALIAS><Opt-CONST><MARK><RESTOFOWNDEC>,
  ;

P<XOWN>=
  "own",
  "external",
  "extrinsic",
  "constant",
  "const";

# An initialised array declaration does not need a %C following the '=' before the list of initial values.
P<CONSTLIST>=
  '='<READLINEq><Opt-PLUS><COPERAND><RESTOFCEXPR><REPFACT><ROCL>,
  ;

P<ROCL>=
  ','<Opt-PLUS><COPERAND><RESTOFCEXPR><REPFACT><ROCL>,
  ;

# "42(2)" is equivalent to "42, 42".  (*) means 'as many more are needed to fill the array' so must come last.
P<REPFACT>=
  '('<Opt-STAROREXPR>')',
  ;

P<RESTOFNLIST>=
  ','<CEXPR><RESTOFNLIST>,
  ;

P<Opt-PercentEVENT>=
  "event",
  ;

# single optional ", expr" for use with %signal event (, subevent)?
P<OPEXPR>=
  ','<EXPR>,
  ;

# rest of %switch list
P<RESTOFSWLIST>=
  ','<NAME><Opt-NLIST>'<'<EXPR>':'<EXPR>'>'<RESTOFSWLIST>,
  ;

P<RESTOFREPEAT>=
  "until"<SC><RESTOFCOND>,
  ;

P<RESTOFSS1>=
  <COLON>,
  <S>,
  <Percent-IU><SC><RESTOFCOND><S>,
  <Percent-WU><S>;

P<RESTOFIU>=
  "start"<NOTESTART>,
  "then" "start"<NOTESTART>,
  "then"<HOLE><UI><MARK><Opt-ELSE>;

# can avoid a start/finish block by call1 %and call2 %if condition
P<AUI>=
  "and"<UI>,
  ;

P<Opt-ELSE>=
  "else"<AFTERELSE>,
  ;

P<AFTERELSE>=
  "start"<NOTESTART>,
  <Percent-IU><HOLE><SC><RESTOFCOND><MARK><RESTOFIU>,
  <UI>;

P<Opt-ENAME>=
  '_'<NAME><APP><Opt-ENAME>,
  ;

P<BPAIR>=
  '('<EXPR>':'<EXPR><RESTOFBPLIST>')';

P<Opt-CONST>=
  '='<Opt-PLUS><COPERAND><RESTOFCEXPR>,
  ;

P<Percent-SEX>=
  "system",
  "external",
  "dynamic",
  ;

P<CYCPARM>=
  <NAME>'='<EXPR>','<EXPR>','<EXPR>,
  ;

# RF in the rules below is short for Record Format
P<RESTOFRFDEC>=
  ','<RFDEC><RESTOFRFDEC>,
  ;

P<RFSTMNT>=
  "spec"<NAME>,
  <NAME>'('<RFDEC><RESTOFRFDEC><ALTRFDEC>')';

P<RFREF>=
  <NAME>,
  <RFDEC><RESTOFRFDEC><ALTRFDEC>;

P<Opt-RFREF>=
  <RFREF>,
  ;

P<RFDEC>=
  <XTYPE><RFELMNT>,
  '('<RFDEC><RESTOFRFDEC><ALTRFDEC>')';

P<RFELMNT>=
  <Opt-PercentQNAME><NAME><Opt-NLIST>,
  "array"<ADECLN>;

P<ALTRFDEC>=
  "or"<RFDEC><RESTOFRFDEC><ALTRFDEC>,
  ;

# a bunch of formats for embedded 2900 assembly language
P<UCI>=
  'PUT_'<ICONST><S>,
  <SETNEM><RESTOFUCI><S>,
  'CNOP_'<N255>','<N255><S>,
  <UCWRONG>;

P<RESTOFUCI>=
  <PRIMFORM><PRIMOP>,
  <SECNFORM>"l="<DRORN><NN>,
  <TERTFORM><N>','<TERTOP>;

P<PRIMOP>=
  '<'<NAME>'>',
  <INDIRECTABLE>,
  '('<INDIRECTABLE><OPTMOD>')',
  '(%DR+'<Opt-INDIRECTABLE>')',
  '('"dr"<OPTMOD>')',
  "b";

P<TERTOP>=
  '<'<NAME>'>',
  <INDIRECTABLE>,
  '('"dr"<OPTMOD>')',
  '(%DR+'<Opt-N>')';

P<DRORN>=
  "dr",
  <N>;

P<NN>=
  ','<N>','<N>,
  ;

P<OPTINC>=
  '+'<N>,
  '-'<N>,
  ;

P<OPTMOD>=
  '+' "b",
  ;

P<Opt-Equals>=
  '=',
  ;

P<INDIRECTABLE>=
  <Opt-Equals><Opt-PLUS><CONST>,
  <NAME><OPTINC>,
  '('<REG><OPTINC>')',
  "tos";

P<Opt-INDIRECTABLE>=
  <INDIRECTABLE>
  ;

P<REG>=
  "lnb",
  "xnb",
  "pc",
  "ctb";

P<UI>=
  <HOLE><NAME><APP><Opt-ENAME><MARK><RESTOFUI><AUI>,
  '->'<NAME><APP>,
  "return",
  "result"<ASSOP><EXPR>,
  "monitor"<AUI>,
  "stop",
  "signal"<Opt-PercentEVENT><CEXPR><OPEXPR>,
  "exit",
  "continue";

P<SS>=
  <CHUI><HOLE><UI><MARK><RESTOFSS1>,
  <TEXT>,
  <Percent-IU><HOLE><SC><RESTOFCOND><MARK><RESTOFIU><S>,
  "finish"<NOTEFINISH><Opt-ELSE><S>,
  "cycle"<NOTECYCLE><CYCPARM><S>,
  "repeat"<NOTEREPEAT><RESTOFREPEAT><S>,
  <Percent-WU>"cycle"<NOTECYCLE><S>,
  <CHXTYPE><BIGHOLE><HOLE><XTYPE><MARK><DECLN><S>,
  "end"<ENDLIST><S>,
  "record" "format"<RFSTMNT><S>,
  <HOLE><Percent-SEX><RT><MARK><Opt-PercentSPEC><NAME><Opt-ALIAS><FPP><S>,
  <XOWN><XTYPE><OWNDEC><S>,
  "include"<CONST><INCLUDE>,
  "begin"<DOWN><S>,
  "on"<TRACE><Opt-PercentEVENT><CEXPR><RESTOFNLIST>"start"
  <NOTESTART><S>,
  "switch"<NAME><Opt-NLIST>'('<EXPR>':'<EXPR>')'
  <RESTOFSWLIST><S>,
  "list"<S><LISTON>,
  "else"<NOTEFINISH><DUMMYSTART><NOTESTART><S>,
  '*'<UCI>,
  "trusted" "program"<S>,
  "main" "ep"<NAME><S>,
  "control"<CONST><S>,
  <NAME>'(*):'<COLON>,
  <S>;

E
@
