Skip to main content
Engineering LibreTexts

17.3: Testing a Grammar

  • Page ID
    43766
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The PetitParser contains a framework dedicated to testing your grammars. Testing a grammar is done by subclassing PPCompositeParserTest as follows:

    Code \(\PageIndex{1}\) (Pharo): Creating a class to hold the tests for our arithmetic expression grammar

    PPCompositeParserTest subclass: #ExpressionGrammarTest
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'PetitTutorial'
    

    It is then important that the test case class references the parser class: this is done by overriding the PPCompositeParserTest»parserClass method in ExpressionGrammarTest:

    Code \(\PageIndex{2}\) (Pharo): Linking our test case class to our parser

    ExpressionGrammarTest>>parserClass
        ^ ExpressionGrammar
    

    Writing a test scenario is done by implementing new methods in ExpressionGrammarTest:

    Code \(\PageIndex{3}\) (Pharo): Implementing tests for our arithmetic expression grammar

    ExpressionGrammarTest>>testNumber
        self parse: '123 ' rule: #number.
    
    ExpressionGrammarTest>>testAdd
        self parse: '123+77' rule: #add.
    

    These tests ensure that the ExpressionGrammar parser can parse some expressions using a specified production rule. Testing the evaluator and pretty printer is similarly easy:

    Code \(\PageIndex{4}\) (Pharo): Testing the evaluator and pretty printer

    ExpressionGrammarTest subclass: #ExpressionEvaluatorTest
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'PetitTutorial'
    
    ExpressionEvaluatorTest>>parserClass
        ^ ExpressionEvaluator
    
    ExpressionEvaluatorTest>>testAdd
        super testAdd.
        self assert: result equals: 200
    
    ExpressionEvaluatorTest>>testNumber
        super testNumber.
        self assert: result equals: 123
    
    ExpressionGrammarTest subclass: #ExpressionPrinterTest
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'PetitTutorial'
    
    ExpressionPrinterTest>>parserClass
        ^ ExpressionPrinter
    
    ExpressionPrinterTest>>testAdd
        super testAdd.
        self assert: result equals: '123 + 77'
    
    ExpressionPrinterTest>>testNumber
        super testNumber.
        self assert: result equals: '123'
    

    This page titled 17.3: Testing a Grammar is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.