Writing a Coco/R Parser with Coco/R - Tutorial

Back to Introduction


In this tutorial a Coco/R parser is created. There is a risk that there will be som confusion: are we talking about some entity in the grammar file or the Coco/R parsing technic? Much care have been taken to minimize this risk.

Writing a parser with Coco/R for Coco/R grammar files

When creating a parser with Coco/R you may step down two paths:

  • Use the builtin (internal) scanner in Coco/R
  • Use an external scanner together with Coco/R parser.
  • Write your own scanner (not recommended).

All have advantages and disadvantages.

Use the Coco/R internal scanner

The advantages of this solution is

  • both scanner and parser rules in the same grammar file.
  • Coco/R does not need any special adoption or that you write glue code.

On the disadvantages

  • Coco/R scanner is not the most powerful scanner (but it is good).

Use the external scanner Flex and its derivatives.

Flex (and its derivatives for other languages) is

  • Have been around for a long time, and thus well tested.
  • Configurable for speed or for minimum memory allocation.
  • May contain states and thus more powerful than a scanner that can't contain states.
  • Very fast and efficient.

On the disadvantages

  • Need a separate grammar file just for Flex.
  • Need glue code to integrate Flex and Coco/R together.

Write your own scanner

The advantages of this solution is

  • Total power on how tokens are created. For some esoteric languages this is the only solution.
  • Possible to optimize more than a general purpose scanner generator.

On the disadvantages

  • Will not be as well tested as the general purpose solutions.
  • Probably more complex to write than learn how to use a general purpose scanner generator.

Down the two paths

In this tutorial both parsers are described:

A hand written scanner will not be covered in this tutorial.


Back to Introduction

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License