Skip to content

Instantly share code, notes, and snippets.

@dherman
Last active December 13, 2015 04:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherman/4871805 to your computer and use it in GitHub Desktop.
Save dherman/4871805 to your computer and use it in GitHub Desktop.
demonstrating that lexing JavaScript requires a pushdown automaton
Looking at the preceding token when the lexer points to a '/'
character is insufficient to determine which context it's in. In
fact, you need to look at an arbitrary number of preceding tokens
to figure it out. This example demonstrates a case where we can
pump up the number of preceding tokens to an arbitrary size
before you can disambiguate your syntactic context.
Function.prototype.valueOf = function() { return 100; };
Number.prototype.test = function() { return true; };
var foo = 2, i = 5;
// ^
// Have to look back to here to figure out
// that we're in an operand position.
function f() { /* arbitrary body */ } /foo/i.test('') && console.log("hello world!")
// ^
// lexer is pointing here
;
Function.prototype.valueOf = function() { return 100; };
Number.prototype.test = function() { return true; };
var foo = 2, i = 5; (
// ^
// Have to look back to here to figure out
// that we're in an operator position.
function f() { /* arbitrary body */ } /foo/i.test('') && console.log("hello world!")
// ^
// lexer is pointing here
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment