Safe Specification of Operator Precedence Rules

  • Ali Afroozeh
  • Mark van den Brand
  • Adrian Johnstone
  • Elizabeth Scott
  • Jurgen Vinju
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8225)


In this paper we present an approach to specifying operator precedence based on declarative disambiguation constructs and an implementation mechanism based on grammar rewriting. We identify a problem with existing generalized context-free parsing and disambiguation technology: generating a correct parser for a language such as OCaml using declarative precedence specification is not possible without resorting to some manual grammar transformation. Our approach provides a fully declarative solution to operator precedence specification for context-free grammars, is independent of any parsing technology, and is safe in that it guarantees that the language of the resulting grammar will be the same as the language of the specification grammar. We evaluate our new approach by specifying the precedence rules from the OCaml reference manual against the highly ambiguous reference grammar and validate the output of our generated parser.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Klint, P., Lämmel, R., Verhoef, C.: Toward an engineering discipline for grammarware. ACM Trans. Softw. Eng. Methodol. 14(3), 331–380 (2005)CrossRefGoogle Scholar
  2. 2.
    Earley, J.: An efficient context-free parsing algorithm. Commun. ACM 13(2), 94–102 (1970)CrossRefMATHGoogle Scholar
  3. 3.
    Tomita, M. (ed.): Generalized LR parsing. Kluwer Academic Publishers (1991)Google Scholar
  4. 4.
    Rekers, J.: Parser Generation for Interactive Environments. PhD thesis, University of Amsterdam, The Netherlands (1992)Google Scholar
  5. 5.
    McPeak, S., Necula, G.C.: Elkhound: A fast, practical GLR parser generator. In: Duesterwald, E. (ed.) CC 2004. LNCS, vol. 2985, pp. 73–88. Springer, Heidelberg (2004)CrossRefGoogle Scholar
  6. 6.
    Baxter, I.D., Pidgeon, C., Mehlich, M.: DMS®: Program transformations for practical scalable software evolution. In: Proceedings of the 26th International Conference on Software Engineering, ICSE 2004, pp. 625–634. IEEE Computer Society, Washington, DC (2004)CrossRefGoogle Scholar
  7. 7.
    Scott, E., Johnstone, A.: GLL parse-tree generation. Science of Computer Programming (2012) (to appear) ISSN:0167-6423Google Scholar
  8. 8.
    Aho, A.V., Johnson, S.C., Ullman, J.D.: Deterministic parsing of ambiguous grammars. In: Proceedings of the 1st Annual ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages, POPL 1973, pp. 1–21. ACM (1973)Google Scholar
  9. 9.
    Aho, A.V., Lam, M.S., Sethi, R., Ullman, J.D.: Compilers: Principles, Techniques, and Tools, 2nd edn. Addison-Wesley Longman Publishing Co., Inc., Boston (2006)Google Scholar
  10. 10.
    Visser, E.: Scannerless generalized-LR parsing. Technical Report P9707, Programming Research Group, University of Amsterdam (July 1997)Google Scholar
  11. 11.
    Klint, P., van der Storm, T., Vinju, J.: EASY meta-programming with rascal. In: Fernandes, J.M., Lämmel, R., Visser, J., Saraiva, J. (eds.) Generative and Transformational Techniques in Software Engineering III. LNCS, vol. 6491, pp. 222–289. Springer, Heidelberg (2011), CrossRefGoogle Scholar
  12. 12.
    Klint, P., Visser, E.: Using filters for the disambiguation of context-free grammars. In: Pighizzini, G., San Pietro, P. (eds.) Proc. ASMICS Workshop on Parsing Theory, Milano, Italy, Tech. Rep. 126–1994, pp. 1–20. Dipartimento di Scienze dell’Informazione, Università di Milano (1994)Google Scholar
  13. 13.
    Visser, E.: Syntax Definition for Language Prototyping. PhD thesis, University of Amsterdam (1997)Google Scholar
  14. 14.
    Aasa, A.: Precedences in specifications and implementations of programming languages. Theor. Comput. Sci. 142(1), 3–26 (1995)MathSciNetCrossRefMATHGoogle Scholar
  15. 15.
    Thorup, M.: Disambiguating grammars by exclusion of sub-parse trees. Acta Informatica 33(5), 511–522 (1996)MathSciNetCrossRefGoogle Scholar
  16. 16.
    Thorup, M.: Controlled grammatic ambiguity. ACM Trans. Program. Lang. Syst. 16(3), 1024–1050 (1994)CrossRefGoogle Scholar
  17. 17.
    Visser, E.: From context-free grammars with priorities to character class grammars. In: van Deursen, A., Brune, M., Heering, J. (eds.) Dat Is Dus Heel Interessant, Liber Amicorum Dedicated to Paul Klint, pp. 217–230. CWI (1997)Google Scholar

Copyright information

© Springer International Publishing Switzerland 2013

Authors and Affiliations

  • Ali Afroozeh
    • 1
  • Mark van den Brand
    • 3
  • Adrian Johnstone
    • 4
  • Elizabeth Scott
    • 4
  • Jurgen Vinju
    • 1
    • 2
  1. 1.Centrum Wiskunde & InformaticaAmsterdamThe Netherlands
  2. 2.INRIA Lille Nord EuropeFrance
  3. 3.Eindhoven University of TechnologyEindhovenThe Netherlands
  4. 4.Royal Holloway, University of LondonEghamUK

Personalised recommendations