1. Give a grammar that defines the language of simple (non-method) function prototypes in C++. Examples of these would be “int f(int x);”, “void g2(int jim, float x);”, “char abc(void);”. In general, you must have a data type, an identifier, a parameter list which consists of zero or more type/name pairs separated by comma’s., and a terminating semicolon. For data types, you should allow char, int, and float. For identifiers (function and parameter names), you should allow any combination of one or more character, where the the first character must be a letter and all other characters may be a letter or a numeric digit. Only include spaces where they must occur (between types and identifiers).
what do you mean by "grammar" ?
A grammar is a system that specifies how you can concatenate the character of alphabet T to form a legal string in a language. Formally a grammar contains four parts: N, a non-terminal alphabet T, a terminal alphabet P, a set of rules of production S, the start symbols which is an element N
<S> -> char <A> | int <A> | float <A> | void <A> <A> -> [A-Za-z]<B>(<C>); | [A-Za-z]<B>(); <B> -> [A-Za-z0-9]<B> | ε <C> -> char <D><E> | int <D><E> | float <D><E> <D> -> [A-Za-z]<B> <E> -> , <C> | ε I think it's correct [A-Za-z] means all letters from A to Z and from a to z, [0-9] numbers from 0 to 9 (I don't know if you can write like this but I don't want to list all those cases) <A> non terminal symbol all other symbols are terminal I constructed it as CFG, context free grammar, I don't know if you need a different one but I haven't learnt anything else
Join our real-time social learning platform and learn together with your friends!