module lang::cobol85::\syntax::Expression
Arithmetic expressions, operands, identifiers, literals and the COPY statement.
Usage
import lang::cobol85::\syntax::Expression;
Dependencies
extend lang::cobol85::\syntax::Lexical;
Description
This module defines the recursive Expression nonterminal for arithmetic: identifiers, literals, brackets,
unary plus and minus, **, *, /, + and -, with the COBOL-85 priorities and associativity. The conditions in
the Condition module continue the same priority chain, so conditions and arithmetic are one nonterminal and a later
checker rejects the expressions COBOL does not allow in a place. Operand is an expression in a list of operands,
such as function arguments, subscripts or the sending fields of DISPLAY and ADD; the EndOperand guard decides by
longest match where one operand ends and the next begins. The module further defines qualified data names with
IN or OF, subscripts and reference modification, figurative constants such as ZERO and HIGH-VALUES, procedure and
section names, and the COPY statement with its REPLACING phrase, which can appear in many places of a program.
syntax CopyStatement
syntax CopyStatement = copy: 'COPY' AltTextNameLiteral CopyPhrase* ".";
syntax AltTextNameLiteral
syntax AltTextNameLiteral
= word: Id
| literal: Literal
;
syntax CopyPhrase
syntax CopyPhrase
= \in: In AltLibraryNameLiteral
| suppress: 'SUPPRESS'
| replacing: 'REPLACING' CopyStatementByPhrase+
;
syntax AltLibraryNameLiteral
syntax AltLibraryNameLiteral
= word: Id
| literal: Literal
;
syntax CopyStatementByPhrase
syntax CopyStatementByPhrase = by: CopyOperand 'BY' CopyOperand;
syntax CopyOperand
syntax CopyOperand
= literal: Literal
| identifier: Identifier
| quotedPseudoText: QuotedPseudoText
// any COBOL word can be replaced, also a reserved word, as in `REPLACING ==...== BY MOVE`
| \keyword: NotFigurative CobwordKeywords !>> [A-Za-z0-9\-]
;
syntax NotFigurative
syntax NotFigurative = () !>> 'ZERO' !>> 'SPACE' !>> 'QUOTE' !>> 'NULL' !>> 'HIGH-VALUE' !>> 'LOW-VALUE';
syntax RecordName
syntax RecordName = qualifiedDataName: QualifiedDataName;
syntax QualifiedDataName
syntax QualifiedDataName
= length: 'LENGTH' 'OF' Identifier
| address: 'ADDRESS' 'OF' Id
| dataName: Id InDataOrFileName*
;
syntax Identifier
syntax Identifier
= linageCounter: 'LINAGE-COUNTER'
| linageCounter: 'LINAGE-COUNTER' In Id
| qualifiedDataName: QualifiedDataName SubscriptParentheses*
| qualifiedDataName: QualifiedDataName SubscriptParentheses* "(" Expression ":" ")"
| qualifiedDataName: QualifiedDataName SubscriptParentheses* "(" Expression ":" Expression ")"
;
syntax SubscriptParentheses
syntax SubscriptParentheses = brackets: "," !<< (", " !<< "(" SubscriptExpression+ ")");
syntax SubscriptExpression
syntax SubscriptExpression = operand: Operand;
syntax Operand
syntax Operand = expression: Expression ! inClass ! notInClass ! zero ! notZero ! conjunction ! disjunction EndOperand;
syntax Expression
syntax Expression
= literal: Literal
| identifier: Identifier
| brackets: "(" Expression ")"
> unaryPlus: "+" Expression
| unaryMinus: [\uE000] !<< "-" Expression
> left power: Expression [\uE000] !<< "**" !>> "*" Expression
> left ( times: Expression [\uE000] !<< "*" !>> "*" Expression | dividedBy: Expression [\uE000] !<< "/" Expression )
> left ( plus: Expression "+" Expression | minus: Expression [\uE000] !<< "-" Expression )
;
syntax InDataOrFileName
syntax InDataOrFileName = \in: In Id;
syntax EndOperand
syntax EndOperand
= () !>> [*/(] !>> "+ " !>> "- " !>> "+\n" !>> "-\n" !>> "+\uE000" !>> "-\uE000"
| "," << () >> "("
| ", " << () >> "("
;
syntax ProcedureName
syntax ProcedureName
= paragraphOrSectionName: ParagraphOrSectionName
| paragraphOrSectionName: ParagraphOrSectionName In SectionName
;
syntax ParagraphOrSectionName
syntax ParagraphOrSectionName
= integer: Integer
| word: Id
;
syntax In
syntax In
= of: 'OF'
| \in: 'IN'
;
syntax SectionName
syntax SectionName
= word: Id
| integer: Integer
;
syntax LiteralThroughLiteral
syntax LiteralThroughLiteral = literal: Literal (Through Literal)?;
syntax Literal
syntax Literal
= \num: Num
| nonnumeric: Nonnumeric
| figurativeConstant: FigurativeConstant
;
syntax FigurativeConstant
syntax FigurativeConstant
= \all: 'ALL' Literal
| figurativeConstantSimple: FigurativeConstantSimple !>> [A-Za-z0-9\-]
// a keyword ends at a word boundary: ZERO-NULL is a data name
;
syntax FigurativeConstantSimple
syntax FigurativeConstantSimple
= spaces: 'SPACES'
| quotes: 'QUOTES'
| highValues: 'HIGH-VALUES'
| null: 'NULL'
| zeroes: 'ZEROES'
| lowValue: 'LOW-VALUE'
| highValue: 'HIGH-VALUE'
| zero: 'ZERO'
| space: 'SPACE'
| quote: 'QUOTE'
| lowValues: 'LOW-VALUES'
| zeros: 'ZEROS'
| nulls: 'NULLS'
;
syntax Through
syntax Through
= through: 'THROUGH'
| thru: 'THRU'
;
syntax ParagraphName
syntax ParagraphName
= word: Id
| integer: Integer
;
syntax Integer
syntax Integer = \num: Num;
syntax AltReelUnit
syntax AltReelUnit
= reel: 'REEL'
| unit: 'UNIT'
;
syntax AltAscendingDescending
syntax AltAscendingDescending
= descending: 'DESCENDING'
| ascending: 'ASCENDING'
;
syntax Records
syntax Records
= records: 'RECORDS'
| record: 'RECORD'
;
syntax Values
syntax Values
= values: 'VALUES'
| \value: 'VALUE'
;
syntax PriorityNumber
syntax PriorityNumber = \num: Num;
syntax EqualTo
syntax EqualTo
= equal: 'EQUAL' 'TO'?
| equalSign: "="
;
syntax CobwordKeywords
keyword CobwordKeywords
= 'ADDRESS'
| 'ALL'
| 'ASCENDING'
| 'BY'
| 'COPY'
| 'DESCENDING'
| 'EQUAL'
| 'HIGH-VALUE'
| 'HIGH-VALUES'
| 'IN'
| 'LENGTH'
| 'LINAGE-COUNTER'
| 'LOW-VALUE'
| 'LOW-VALUES'
| 'NULL'
| 'NULLS'
| 'OF'
| 'QUOTE'
| 'QUOTES'
| 'RECORD'
| 'RECORDS'
| 'REEL'
| 'REPLACING'
| 'SPACE'
| 'SPACES'
| 'SUPPRESS'
| 'THROUGH'
| 'THRU'
| 'TO'
| 'UNIT'
| 'VALUE'
| 'VALUES'
| 'ZERO'
| 'ZEROES'
| 'ZEROS'
;