120 likes | 203 Views
Testing Grammars For Top Down Parsers. By Asma M Paracha, Frantisek F. Franek Dept. of Computing & Software McMaster University Hamilton, Ont. Presentation Outline. Background Information Purdom’s Algorithm Implementation of Purdom’s Algorithm Test cases for MACS
E N D
Testing Grammars For Top Down Parsers By Asma M Paracha, Frantisek F. Franek Dept. of Computing & Software McMaster University Hamilton, Ont
Presentation Outline • Background Information • Purdom’s Algorithm • Implementation of Purdom’s Algorithm • Test cases for MACS • Conclusions & Future Work
Compiler Test Case • Compiler is a computer program which accepts a source program as input and produces either an object code or error messages depending upon the source code. • Test case for a compiler should have: • A test case description • A source program • An expected output • Major Problem • Completeness of coverage • Unfeasible size of test data
Testing Grammar • Grammar defines both a language and the basis of deriving elements for a language. • Grammar is considered both as a program and a specification. • Two important types of grammars: • Context free grammars • Regular grammars • Grammar should be tested to verify: • It defines the same language for which it is written • For completeness (every terminal and every rule is used) • To remove ambiguity.
Context Free Grammars (CFG) • CFG is a set of recursive rewriting rules used to generate strings in various patterns. • Components of grammar: • N: Finite set of non-terminals • T: Finite set of terminals, does not intersect with N • S: Initial symbol from N (starting symbol) • P: Finite set of production rules of the form n m • No practical way to check the dynamic semantics of the language defined by CFG.
Testing Parser • The process of checking a given program against the grammar rules to determine whether or not it is syntactically correct. • Test data for parser is a program which use all the production rules of underlying grammar. Testing the parser
Purdom’s Algorithm • Proposed by Purdom in 1972 for testing compilers for automatically generating sentences from the grammar by using every production rule at least once. • Sentences generated are good to check most of compiler code and tables. • Only checks the syntactic aspect, no guarantee for the proper execution. • Generated sentences may be inconsistent with the contextual constraints for e.g. variable declarations, use of identifiers, type checking. • Purdom’s algorithm verifies the compiler correctness, not interested in other aspects such as: • Efficiency • Performance
Testing MACS Compiler • MACS is a simple object oriented language very much similar to C++ and Java. It is used in the forth coming book of Franek on compilers. • MACS uses both LALR and LL(1) grammars and has a C++ bottom-up parser built using Bison/Flex and a Java top-down parser built using JavaCC. • MACS LL(1) grammar has 77 terminlas,90 non-terminals and 301 production rules.
Conclusions • Parser testing is an area which needs more attention. • Purdom’s algorithm is a complete method for testing small grammars. • Test cases generated are incorporated with the semantic aspects of the language to perform the compiler validation. • Future work includes testing the most advanced features of MACS compiler.