2nd Chapter – Describing Syntax and Semantics

In this chapter we’ll go further on Syntax and Semantics.

But before that what is Syntax and Semantics ?

Syntax:  the structure of the code in a programming language. Much like grammar.

Semantics: the meaning of the code which are written. Like the reason it is written.

 

The formal method of describing syntax is going with the terminology which is the defined words of what is written, such as sentence, language, lexeme, token, etc. Thus

 

Based on Backus-Naur Form and Context-Free Grammar, you can derivate what is written in the code. Like as the following:

<program> => <stmts> => <stmt>

=> <var> = <expr>

=> a = <expr>

=> a = <term> + <term>

=> a = <var> + <term>

=> a = b + <term>

=> a = b + const

 

Attribute Grammars

A context-free grammar G = (S, N, T, P) with the following additions:

–For each grammar symbol x there is a set A(x) of attribute values

–Each rule has a set of functions that define certain attributes of the nonterminals in the rule

–Each rule has a (possibly empty) set of predicates to check for attribute consistency

and the example:

Syntax

<assign> -> <var> = <expr>

<expr> -> <var> + <var> | <var>

<var> A | B | C

  • actual_type: synthesized for <var> and <expr>
  • expected_type: inherited for <expr>

 

Semantics

So far there are no widely acceptable notation of describing this. So go with what I’ve defined on the top ^^^.

Several needs for a methodology and notation for semantics:

–Programmers need to know what statements mean

–Compiler writers must know exactly what language constructs do

–Correctness proofs would be possible

–Compiler generators would be possible

–Designers could detect ambiguities and inconsistencies

List of semantics and what they are:

Operational Semantics: Describing the meaning of a program by executing it.

Denotational Semantics: Based on recursive function theory and the most abstract semantics.

Axiomatic Semantics: Semantics which is based on formal logic (predicate calculus)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *