Skip to content

Instantly share code, notes, and snippets.

@suguanYang
Created May 5, 2019 07:36
Show Gist options
  • Save suguanYang/ec35c74942ce71dbdeceda338b48bd06 to your computer and use it in GitHub Desktop.
Save suguanYang/ec35c74942ce71dbdeceda338b48bd06 to your computer and use it in GitHub Desktop.
Free-context grammar

What is grammar?

Grammas are the language of languages. A grammar defines a language. Behind every language, there is a grammar that determines its structure.

Context-free grammar

In computer science, the most common type of grammar is the context-free grammar. Context-free grammars have sufficent richness to describle the recursive syntactic structure of many languages.

Components of a context-free grammar.

A set of rules is the core components of a grammar. Each rule has two parts: (1)a name and (2) an expression of the name. For instance, if we were creating a grammar to handle english text, we might add a rule like: noun-phras may expand into article noun

from which we could ultimately deduce the that 'the dog' is a noun-phrase. Or, if we were describing a programming language, we could add a rule like: expression may expand into expression + expression

If we're looking with grammars as mathematical objects, thne instead of writing 'may expand into', we'd simplt write ->: noun-phrase -> article noun expression -> expression + expression

As an example, consider the classic unambiguouse expression grammar: expr -> term + expr expr -> term term -> term * factor term -> factor factor -> ( expr ) factor -> const const -> integer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment