Docsity

Generating Programs with Grammars and ASTGen: A Comprehensive Overview, Study notes of Computer Science

Partial preview of the text

Download Generating Programs with Grammars and ASTGen: A Comprehensive Overview and more Study notes Computer Science in PDF only on Docsity!

Generating Programs with

Grammars and ASTGen

598DM Guest Lecture

Brett Daniel

Tuesday, March 11, 2008

Overview

Grammar

Program Text Parser Parse Tree

AST Nodes

parsed by

language for

produces

composed of

Grammar-Based Testing

● Produce many programs from grammar

● Used to test parser/compiler

Parse Tree

AST Nodes

produces

(roughly) corresponds to

generates composed of

Parser

language for

parsed by

Grammar

Program Text

Example: Fields

Field Declaration

Field References

class A { int f;

void m( int i) { f = i * f; } }

Terminal Symbol Coverage

● The set of test inputs contains each terminal

symbol

FieldDeclaration ::= Modifier Type Identifier “;”

Modifer ::= “public” | “protected” | “private” | ε

Type ::= BasicType | Identifier

BasicType ::= “int” | “boolean”

Identifier ::= [a-zA-Z_]+

Terminal Symbol Coverage

● The set of test inputs contains each terminal

symbol

FieldDeclaration ::= Modifier Type Identifier “;”

Modifer ::= “public” | “protected” | “private” | ε

Type ::= BasicType | Identifier

BasicType ::= “int” | “boolean”

Identifier ::= [a-zA-Z_]+

Derivation Coverage

● The set of test inputs contains every possible

string

public

protected

private

int

boolean

× ×

Modifier Type^ Identifier

Grammars and ASTs

● Grammar roughly corresponds to AST class

structure

Program Text parsed by Parser Parse Tree

language for

produces

(roughly) corresponds to

composed of

Grammar AST Nodes

Grammars and ASTs

Grammar AST Nodes

(roughly) corresponds to

FieldDeclaration ::= Modifier Type Identifier “;”

Modifer ::= “public” | “protected” | “private” | ε

Type ::= BasicType | Identifier

BasicType ::= “int” | “boolean”

Identifier ::= [a-zA-Z_]+

Abstract Grammars

● Grammar used to generate AST class and

composition structure

FieldDeclaration ::= Modifier Type Identifier;

Modifer;

abstract Type; BasicType : Type; SimpleType : Type ::= Identifier;

Identifier ::= <Name:String>;

What About Field References?

● Difficult to define grammar for all types of field

references

  • (^) Want both read and write references
  • (^) Expressions depend on field name
  • (^) Productions spread across language grammar
  • (^) Multiple classes, inheritance relationships,

containment, etc.

● Alternative: “Extended Grammars”

Generating ASTs

● ASTGen generators produce many AST Nodes

● Combining generators produces

many parse trees

Program Text parsed by Parser

language for

produces

(roughly) corresponds to

composed of

ASTGen

generates

Grammar AST Nodes

Parse Tree

Generator Characteristics

● Iterative

  • (^) Act like collection of AST fragments
  • (^) Internally, produce fragments lazily

● Composable

  • (^) Tester creates complex generators by reusing

simpler parts

  • (^) Generators contain code to combine AST fragments

● Bounded-Exhaustive

  • (^) Achieves derivation coverage within bounds

Field Declaration Generator

Iterates children and

builds AST node

Instances supplied by caller