Showing posts with label Compiler Theory. Show all posts
Showing posts with label Compiler Theory. Show all posts

Saturday, March 19, 2011

Three extension are common to most EBNFs

  • The first extension denotes an optional part of a RHS, which is delimited by brackets. For example:   -> if ( ) statement [ else ]
  • The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether. For example:  -> identifier {, }
  • The third extension deals with multiple choice options. For example:  -> term ( *| / | % )

Distinguish between static and dynamic semantics

  • Static semantic of a language is only indirectly related to the meaning of program during execution; rather it has to do with the legal forms of programs (syntax rather than semantics). Many static semantic rules of a language state its type constraints. Static semantics is so named because the analysis is required to check these specification can be done at compile time.
  • Dynamic semantics is the meaning of the expression, statements, and program units of a programming language.
Generally speaking, dynamic semantics are defined during execution time and can be change while static semantics are known at compile time and do not change.

What are the advantages and disadvantages of dynamic scoping?

Disadvantages

  • Inability to statically check for references to nonlocal variables.
  • Dynamics scoping also makes program difficult to read because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables. 
  • There's no way to protect local variables from being accessed to by subprograms because local variables of subprogram are all visible to all other executing subprograms, regardless of textual proximity.
  • Accessing to nonlocal variables in dynamic scoping takes far longer than accesses to nonlocal variables when static scoping is used.
Advantages
  • On the other hand, the only advantage of dynamic scoping is writability. It is more convenient and it provides more flexibility than static scoping. For example, in some cases, some parameters passed from one subprogram to another are variables that are defined in the caller. None of these need to be passed in a dynamic scoped language, because they are implicitly visible in the called subprogram.

Three main approaches to build a lexical analyzer

  1. Write a formal description of the token patterns of the language using descriptive languages related to regular expression. These descriptions can be used as inputs to a software tool such as Yac, Lex, that automatically generate a lexical analyzer.
  2. Design a state transition diagram that describes the token patterns of the language and write a program that implements the diagram.
  3. Hand construct a table driven implementation of the state diagram.