question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow syntactic rule application inside a lexical rule

See original GitHub issue

As discussed on this thread in the Google Group:

Take JS’s template strings, for example:

`The answer is ${6 * 7}`

To parse this, it would be nice if you could write something like:

tstr
  = "`" tstrPart* "`"

tstrPart
  = "${" Expr "}"      -- expr
  | ~("`" | "${") any  -- char

Right now you can’t do this, because that invocation of Expr is not allowed. But it could be.

Yesterday Pat and I talked about this, and we think that it might be a good idea to allow syntactic rules to be invoked from inside lexical rules – this would probably require an operator, something like the analog of the “lexification” operator, #. For now, I’ll use @.

We could rewrite the tstrPart rule above using @ as follows:

tstrPart
  = "${" @Expr "}"      -- expr
  | ~("`" | "${") any   -- char

The semantics of @ would be the same as the semantics of the application of a start rule that is syntactic. In other words, @Expr would skip both leading and trailing whitespace. (That’s exactly what you’d want in this example.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
pdubroycommented, Nov 3, 2021

This has shipped in an experimental form in v16.1.0-pre.1, which can be installed via npm install ohm-js@next. It’s also available in the Ohm Editor by appending ?ohm-js=next to the URL.

Here is a sample grammar, using the tstr example that has been used in this thread:

G {
  tstr = "`" tstrPart* "`"

  tstrPart
    = "${" experimentalApplySyntactic<Expr> "}"  -- expr
    | ~("`" | "${") any  -- char
  
  Expr = number "+" number
  number = digit+
}

Note that both leading and trailing whitespace will be skipped around Expr. Here is a screenshot from the editor with Show spaces enabled:

Screen Shot 2021-11-03 at 12 48 01

Feedback on this is very much welcome!

1reaction
pdubroycommented, Jul 4, 2021

@4silvertooth That’s an interesting case, but I don’t think we need to support it. You can always use ~space inside a lexical context to disallow spaces (and prevent space skipping) at that position. If we had the bang operator that I described above:

tstrPart
  = "${" ~space Expr! "}"  -- expr
  | ~("`" | "${") any      -- char

…should do the trick.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LEXICAL AND SYNTACTIC RULES IN A TREE ADJOINING ...
The main verb 'give' seems to behave syntactically and semantically as in non idiomatic constructions: Dative shift applies and we have the regular...
Read more >
Lexical rule - Wikipedia
A lexical rule is in a form of syntactic rule used within many theories of natural language syntax. These rules alter the argument...
Read more >
Grammar: syntax and the lexicon
Let's start with phrase structure rules. ... reasonable to suggest that the above rules and other similar rules apply in the formation of...
Read more >
Syntax Reference - Ohm
A syntactic rule is a rule whose name begins with an uppercase letter, and lexical rule is one whose name begins with a...
Read more >
(PDF) Controlling the Application of Lexical Rules
Recently, techniques have been described which address the efficiency issues that this raises for fully productive rules, such as inflectional ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found